How to create user login/Sign-in Webservice in PHP
In previous tutorial we learned , How to create user registration/signup Webservice in PHP. Now, in this tutorial we will learn how to user login/sign in webservice.
MySQL Table Structure for signup Table(This table also used in the previous tutorial)
CREATE TABLE signup (
id int(11) NOT NULL,
fullName varchar(120) DEFAULT NULL,
gender varchar(100) DEFAULT NULL,
contactNumber char(10) DEFAULT NULL,
email varchar(150) DEFAULT NULL,
password varchar(200) DEFAULT NULL,
regDate timestamp NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE signup
ADD PRIMARY KEY (id);
Create a database connection file (config.php)
<?php
// DB credentials.
define('DB_HOST','localhost');
define('DB_USER','root');
define('DB_PASS','');
define('DB_NAME','database Name'); // put you database name here
// Establish database connection.
try
{
$dbh = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME,DB_USER, DB_PASS,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));
}
catch (PDOException $e)
{
exit("Error: " . $e->getMessage());
}
?>
Create a requested URL (http://localhost/ws/signin.php)
<?php
include_once("config.php");
$usignin=json_decode(file_get_contents('php://input'),true);
$email=$usignin['email'];
$pass=$usignin['pwd'];
$sql ="SELECT id,fullName FROM signup WHERE email=:uemal and password=:upass";
$query= $dbh->prepare($sql);
$query->bindParam(':uemal',$email, PDO::PARAM_STR);
$query->bindParam(':upass',$pass, PDO::PARAM_STR);
$query-> execute();
$results= $query -> fetchAll(PDO::FETCH_OBJ);
if($query -> rowCount() > 0)
{
foreach($results as $reslt){
$userid=$reslt->id;
$fname=$reslt->fullName;
}
$msg['StatusCode']="200";
$msg['StatusDescription']=array("UserId"=>$userid,"FullName"=>$fname);
echo json_encode($msg);
}else{
header("HTTP/1.1 400 Bad Request");
header('Content-Type: text/plain');
$msg['StatusCode']="300";
$msg['StatusDescription']="Invalid Details";
echo json_encode($msg);
}
?>
header(“Content-type: application/JSON”) sends the HTTP JSON header to the browser to inform him what kind of data he expects.
json_decode(file_get_contents(‘php://input’),true) receive the RAW post data via the php://input IO stream.
We retrieve the raw POST data from the php://input stream.
header(….) detect http response line.
header(“HTTP/1.1 200 OK”); // for successful attempt
header(“HTTP/1.1 400 Bad Request”); // for bad request
header(‘Content-Type: text/plain’) indicates that the content is plain text and no special software is required to read the contents.
How to test this service in your local system
- Download the postman app from here https://www.postman.com/downloads/
- Run postman app
- Choose HTTP post method
- Enter the requested URL (http://localhost/ws/signin.php)
- Provide the requested data in JSON Format
- Hit the send button
Input parameters will be like this
{
“email”:”john@anc.com”,
“pwd”: “Demo”
}
Output will be in JSON format
For successful attempt
{“StatusCode”:”200″,”StatusDescription”:{“UserId”:”1″,”FullName”:”John Doe”}}
For unsuccessful attempt
For unsuccessful attempt
{“StatusCode”:”300″,”StatusDescription”:”Invalid Details”}
