All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Create Login API In PHP JSON

Last Updated : Mar 11, 2024

How To Create Login API In PHP JSON

In this article we will show you the solution of how to create login API in PHP JSON, API stands for Application Programming Interface. We can create a Login API using php JWT.

Now let us understand what is JWT first.

JSON Web Token

JWT also known as JSON Web token provides you a way of representing claims to be transferred between two parties.

It is mainly used for the authorization process. JWT consists of three parts – header, payload, and signature.

The header and the payload are base64 encoded strings that can be decoded without any need for a password.

Step By Step Guide On How To Create Login API In PHP JSON :-

Let us see an example on creating a login API first.

<?php
header ('Access-Control-Allow-Origin:*') ;
header ('Access-Control-Allow-Method:POST') ;
header ('Content-Type:application/json') ;
include '../database/Database.php' ;
include '../vendor/autoload.php' ;
use \Firebase\JWT\JWT ;
$obj = new Database() ;
if($_SERVER['REQUEST_METHOD'] == 'POST') {
    $data = json_decode(File_get_contents("php://input", true)) ;
    $email = htmlentities($data->email) ;
    $password= htmlentities($data->password) ;
    $obj->select('users', '*', null, "email='{$email}'",null, null) ;
    $datas = $obj -> getResult() ;
    foreach ($datas as $data) {
        $id = $data['id'] ;
        $name = $data['name'] ;
        $email = $data['email'] ;
        if(!password_verify($password,$data['password'])) {
            echo json_encode ([
                'status' => 0 ,
                'message' => 'Invalid Carditional'
            ]) ;
        } else {
             $payload = [
            'iss' => "localhost" ,
            'aud' => 'localhost' ,
            'exp'=>time()+1000 ,
            'data' =>[
                'id' => $id ,
                'name' => $name ,
                'email' => $email ,
            ],
        ] ;
        $secret_key="TalkersCode"
        $jwt = JWT::encode ($payload,$secret_key,'HS256') ;
        echo json_encode ([
            'status' => 1 ,
            'jwt' => $jwt ,
            'message' => 'Login succesfully'
        ]) ;
        }
    }
}
else {
    echo json_encode ([
        'status' => 0 ,
        'message' => 'Access Denied'
    ]) ;
}
?>
  1. First create a <?php tag to write php within it.
  2. Add some header files within the header() tag.
  3. Using include a statement to take the codes from specified files
  4. Create a ‘Use’ keyword
  5. Create a variable named $obj with new Database()
  6. Now create an if statement with a Request_method to POST.
  7. Within the if statement, $data with a json_decode() which returns the value encoded in json. Now using file_get_contents() function. Using $email and $password variable with htmlentities () function.
  8. Using the variable $obj with select() function
  9. $datas with $obj to getResult()
  10. Now create a foreach loop with $datas as $data. Using $id, $name, $email with the $data[] variable.
  11. Again create a if statement for the password verification. Password_verify() function show an error the using echo to json_encode() that status to 0, message to Invalid Craditional .
  12. Now create a else statement for the ‘Payload’. Set the ‘iss’ to ‘localhost, ‘aud’ to ‘localhost’, ‘exp’ to time()+1000 and the ‘data’ to previous id,name and email variable.
  13. Set the $secret_key to ‘TalkersCode’
  14. JWT::Encode() with $payload,$secret_key and the algorithm of ‘HS256’.
  15. Again using echo to json_encode() with the status to 1, jwt to $jwt, and with the message of ‘Login successfully’
  16. Create the final else statement, within it echo json_encode() with status to 0 and message of ‘Access Denied’
  17. Close the php code with ?> tag.

Conclusion :-

At last, here in conclusion, here we can say that with this article’s help, we know how to create login API in php using JWT.

I hope this article on how to create login API in PHP JSON helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪