login api using php

In this article, we will discuss about web service or Rest API and create a simple login web service or Rest API using PHP, Mysql, and JSON.

What is web service or Rest API ?

A web service is a way of communication between a client-server application or application component for communication. Web service is any piece of software or application that provides standardized web protocols (HTTP or HTTPS) to interoperate, communicate, and exchange data messaging. API and Web service serve as a means of communication.

Example –

Create a Database –

First of all, create a database and named it as Test, after this, we create a Table named as user having six Columns.

In our last tutorial, we have discussed about How to create a Registration web service / Rest API using Php, Mysql, and JSON.

Connecting Php with Mysql Database –

Create a file named Config.php and use it to write the configuration details of the database.

Config.php

<?php
define('hostname','localhost');
define('user','root');
define('password','root');
define('db_name','Test');
?>

Inside Config.php file the database user name is root and the password is also root, and the database name is Test.

Now we create a file named Con.php to create a connection with the database.

Con.php

<?php
require_once 'config.php';
class Connection{
   private $connect;
   function __construct(){
   $this->connect=mysqli_connect(hostname,user,password,db_name) or die("DB Connection error.");
   }
   public function get_connection()
   {
       return $this->connect;
    }
}
      
 ?>

After this, we are going create another file named login.php, where we will handle all the API calls.

<?php
include('con.php');
header('Content-Type: application/json');

class User
{
private $db;
private $connection;
function __construct()
{
$this->db = new DB_Connection();
$this->connection=$this->db->get_connection();
}
public function does_user_exist($mail,$pass){
$query = "SELECT * FROM user WHERE email='$mail' AND password='$pass'";
$result=mysqli_query($this->connection,$query);
if(mysqli_num_rows($result)>0){
$row= mysqli_fetch_array($result);
$data = array(); 
array_push($data,array( "user_id"=>$row['user_id'],
"name"=>$row['name'],
"email"=>$row['email'],
"mobile"=>$row['mobile'],
"address"=>$row['address'],
"password"=>$row['password'], ) );

$json['status']=200;
$json['message']='Login Successful';
$json['detail']=$data;

echo json_encode($json);

mysqli_close($this->connection);
}else { 
$json['status']=400;
$json['message']='Wrong email or password';
echo json_encode($json);
mysqli_close($this->connection);
}
}
}

$user = new User();
if(isset($_POST['email'],$_POST['password']))
{
$mail=$_POST['email'];
$pass=$_POST['password'];
if (!empty($mail) && !empty($pass)) {
$encrypted_password=md5($pass);
$user-> does_user_exist($mail,$encrypted_password);
}else {
$json['status']=100;
$json['message']='You must fill both fields';
echo json_encode($json);
}
}
?>

Now we can test it using Postman with the given key name email, and password.

%d bloggers like this: