PHP code example of linwanfeng / jwt-php

1. Go to this page and download the library: Download linwanfeng/jwt-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

linwanfeng / jwt-php example snippets



use \JwtPhp\Jwt;
$jwt = new Jwt();
//生成签名
$jwt->setSecret("xDE}o4L1dVW+'@;P#=9]SFvVzPs'~Z");//设置密钥
$jwt->setExp(time()-1);
$jwt->setIat(time()-2);
try {
    $jwt->setPublicPd(['username' => 'linwanfeng', 'luserid' => 'Ahkjhkgusd']);//设置公共信息
    $b = $jwt->genSignature();//生成jwt字符串
} catch (Exception $e) {
    echo "必须传数组";
}

try {
    $a = $jwt->vailDecSign("sdhlshadhsaidhhhhhhhhhhhhhhhhhhhhhhhhhhhhhsaidh.sadsadsad.saddssssssssssssssssssssss");
} catch (Exception $e) {
}
if ($a===true) {
    echo "验证通过";
}

$exp = $jwt->isExp();//调用此方法之前必须验证token真实性,return true or false
if ($exp) {
    echo "token未过期";
} else {
    echo "token已过期";
}

$jwt->getRecPd();//获取全部信息
$jwt->getRecPd('username');//获取某个字段信息