PHP sample code to create an authorisation key using JSON Web Tokens (JWT)

We know that API play an important role for connecting or transferring data between different system or application with the server,when we are dealing with customer data one of with biggest issue is the security.we need proper authorization to exchange data between the system. Fortunatily we have JSON Web Tokens(JWT) it helps us to create a authorization key and verify the authorization key. here we are checking out how to create a authorization key using JSON Web Tokens(JWT)in PHP.

Let we check in steps

Steps 1

Download JSON Web Tokens library from below link 
https://jwt.io/     download for PHP

Steps2

Create public key and private key


// Create the keypair
$res=openssl_pkey_new();

// Get private key
openssl_pkey_export($res, $privateKey);

// Get public key
$pubkey=openssl_pkey_get_details($res);
$publicKey=$pubkey["key"];

Step 3

Create a PHP page with below code to generate authorisation key


require_once PROJECT_ROOT.'/php-jwt/src/BeforeValidException.php';
require_once PROJECT_ROOT.'/php-jwt/src/ExpiredException.php';
require_once PROJECT_ROOT.'/php-jwt/src/SignatureInvalidException.php';
require_once PROJECT_ROOT.'/php-jwt/src/JWT.php';
use \Firebase\JWT\JWT;
$token_array=array();
$token_array['time']=time();
$token_array['user_name']="sachin";
$token_array['user_id']="144";
$token_array['mobile']="*********";
$token_array['admin']=false;
$authorisation_key=JWT::encode($token_array, $privateKey, 'RS256');

echo $authorisation_key;

Step 4

Create a PHP page with below code to verify authorisation key


require_once PROJECT_ROOT.'/php-jwt/src/BeforeValidException.php';
require_once PROJECT_ROOT.'/php-jwt/src/ExpiredException.php';
require_once PROJECT_ROOT.'/php-jwt/src/SignatureInvalidException.php';
require_once PROJECT_ROOT.'/php-jwt/src/JWT.php';
use \Firebase\JWT\JWT;
$token_array=array();
$token_array['time']=time();
$token_array['user_name']="sachin";
$token_array['user_id']="144";
$token_array['mobile']="*********";
$token_array['admin']=false;
$decoded = JWT::decode($token, $publicKey, array('RS256'));
print_r((array) $decoded);

How to insert new key value pair inside a multidimensional associative array in PHP

To insert new key value pair inside a multidimensional associative array in PHP
Here am going to explain in details
We have a sample array we need to add Version4 and its value inside the array

$list_array = array( 
            0 => array (
               "Version1" => 2016,
               "Version2" => 2017,	
               "Version3" => 2018
            ),
            
            1 => array (
               "Version1" => 2016,
               "Version2" => 2017,	
               "Version3" => 2018
            )
         );

Here am using for each mange that

 foreach($list_array as $key=>$valuue)
	 {
	  $list[$key]=$listvalue;
	  $list[$key]['Version4']= 2019;
	 }
       print_r($list);

Result

 
array( 
            0 => array (
               "Version1" => 2016,
               "Version2" => 2017,	
               "Version3" => 2018,
               "Version4" => 2019
          
              
            ),
            
            1 => array (
               "Version1" => 2016,
               "Version2" => 2017,	
               "Version3" => 2018,
               "Version4" => 2019
            )
         );

Sample php code for sending OTP

Most of the user oriented website are slowly changing from email and password based login to OTP based login.so for a developer it is necessary to know how to create on OTP based login, here am going to share you Sample PHP code for sending OTP.

First you have to Register with any of the SMS Service providers ,here am using mysmsmantra.Here we are achieving with the help of CURL in PHP,this is the URL API from mysmsmantra for sedning OTP http://bulksms.mysmsmantra.com/WebSMS/SMSAPI.jsp?username=YOUR_USERNAME&password=YOUR_PASSWORD&sendername=rahaul&mobileno=*******&message=your OTP
where YOUR_USERNAME and YOUR_PASSWORD are respectively user name and password from mysmsmantra.

$otp=rand(100000, 999999);
$msg="Dear Customer, One Time Password for activating your account is :".$otp;
$message=urlencode($msg); 
$messageUrl="http://bulksms.mysmsmantra.com/WebSMS/SMSAPI.jsp?username=YOUR_USERNAME&password=YOUR_PASSWORD&sendername=rahaul&mobileno=*******&message=".$message; 
//YOUR_USERNAME and YOUR_PASSWORD are respectively from mysmsmantra
$ch = curl_init($messageUrl); 
$fp = fopen("message.txt", "w+"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_FILE, $fp); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_exec($ch); curl_close($ch); 

fclose($fp);

Usage of for loop in Jquery syntax with example

Here am going to explain the use of for loop in JQuery with an example

Add a page ajax_angular_view.php with response format

{"Company1":"IBM","Company2":"TCL","Company3":"GOOGLE"}

HTML Area

 <div><input id="update_company_name_comapny1" type="text" value="" /></div>
<div><input id="update_company_name_comapny2" type="text" value="" /></div>
<div><input id="update_company_name_comapny3" type="text" value="" /></div>
var company_id=1
$.ajax({
                url: "ajax_angular_view.php?action=view_company_info_by_id&company_id="+company_id,
                type: "post",
                success: function(data) {
					
                    var obj = JSON.parse(data);
		for(var k in obj) {
					$("#update_company_name_"+k).val(obj[k])
 
                     }
		console.log(obj);
                     
                }
            });