如何使用token 服务器手机php 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 token 用来验证 请求是属于你的客户端发出的 只有token 校验为true时服务器才会接受请求 返回数据 那个token就是一个令牌,用来判断身份的。首先服务器创建一个token传到客户端,客户端每次请求,都把token传给服务器验证身份。来一个简单的,将id加密为token,然后把token解密为id<?phpdefine('KEY', '#^DKHSD&*F'); // 定义密钥$id = '123';$token = genToken($id);echo 'id encrypt token='.$token.'<br>';echo 'token decrypt id='.checkToken($token);// 创建tokenfunction genToken($id){ $str = json_encode(array(time(),mt_rand(1000,9999),$id)); return authcode($str, 'ENCODE', KEY);}// 验证tokenfunction checkToken($token){ $str = authcode($token, 'DECODE', KEY); $result = json_decode($str); if(is_array($result)){ return $result[2]; }else{ return 'decrypt fail'; }}// 加密/解密方法function authcode($string, $operation = 'DECODE', $key){ $ckey_length = 4; // 随机密钥长度 取值 0-32; $key = md5($key); $keya = md5(substr($key, 0, 16)); $keyb = md5(substr($key, 16, 16)); $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : ''; $cryptkey = $keya.md5($keya.$keyc); $key_length = strlen($cryptkey); $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', 0).substr(md5($string.$keyb), 0, 16).$string; $string_length = strlen($string); $result = ''; $box = range(0, 255); $rndkey = array(); for($i = 0; $i <= 255; $i++) { $rndkey[$i] = ord($cryptkey[$i % $key_length]); } for($j = $i = 0; $i < 256; $i++) { $j = ($j + $box[$i] + $rndkey[$i]) % 256; $tmp = $box[$i]; $box[$i] = $box[$j]; $box[$j] = $tmp; } for($a = $j = $i = 0; $i < $string_length; $i++) { $a = ($a + 1) % 256; $j = ($j + $box[$a]) % 256; $tmp = $box[$a]; $box[$a] = $box[$j]; $box[$j] = $tmp; $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256])); } if($operation == 'DECODE') { if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) { return substr($result, 26); } else { return ''; } } else { return $keyc.str_replace('=', '', base64_encode($result)); }}?> token 令牌, 客户端和服务端 相互验证,即加密和解密生成加密字符串即可。 服务器是每次请求都创建一个新的token,还是就第一次请求时创建一次就可以了? 服务器是每次请求都创建一个新的token,还是就第一次请求时创建一次就可以了? 如果检测到有 token,那么这个 token 可以继续使用不过 token 本意是防止恶意访问,复用就有可被利用的漏洞所以还是每次发放比较好 关于读取RSS数据 在一个已经建好的网站上添加一个站内搜索功能!求达人协助,有重谢! phpwind 二次开发的一个小问题 上传图片到服务器,路径问题 求一个由国内开发且比较好的PHP开源文件管理系统 有做过DEDE CMS 的吗?帮忙看看? 使用接口注册域名 请问phpweb登陆后台老是提示验证码图形错误是什么原因 微信支付提示 调用支付JSAPI缺少参数:appId 能看下这个抽签的源码大概是什么意思 YII 中的这段代码如何写? php查询mysql后如何合并相同字段值再输出
define('KEY', '#^DKHSD&*F'); // 定义密钥$id = '123';
$token = genToken($id);echo 'id encrypt token='.$token.'<br>';
echo 'token decrypt id='.checkToken($token);// 创建token
function genToken($id){
$str = json_encode(array(time(),mt_rand(1000,9999),$id));
return authcode($str, 'ENCODE', KEY);
}// 验证token
function checkToken($token){
$str = authcode($token, 'DECODE', KEY);
$result = json_decode($str);
if(is_array($result)){
return $result[2];
}else{
return 'decrypt fail';
}
}
// 加密/解密方法
function authcode($string, $operation = 'DECODE', $key){ $ckey_length = 4; // 随机密钥长度 取值 0-32; $key = md5($key);
$keya = md5(substr($key, 0, 16));
$keyb = md5(substr($key, 16, 16));
$keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : ''; $cryptkey = $keya.md5($keya.$keyc);
$key_length = strlen($cryptkey); $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', 0).substr(md5($string.$keyb), 0, 16).$string;
$string_length = strlen($string); $result = '';
$box = range(0, 255); $rndkey = array();
for($i = 0; $i <= 255; $i++) {
$rndkey[$i] = ord($cryptkey[$i % $key_length]);
} for($j = $i = 0; $i < 256; $i++) {
$j = ($j + $box[$i] + $rndkey[$i]) % 256;
$tmp = $box[$i];
$box[$i] = $box[$j];
$box[$j] = $tmp;
} for($a = $j = $i = 0; $i < $string_length; $i++) {
$a = ($a + 1) % 256;
$j = ($j + $box[$a]) % 256;
$tmp = $box[$a];
$box[$a] = $box[$j];
$box[$j] = $tmp;
$result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
} if($operation == 'DECODE') {
if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {
return substr($result, 26);
} else {
return '';
}
} else {
return $keyc.str_replace('=', '', base64_encode($result));
}}
?>
生成加密字符串即可。
不过 token 本意是防止恶意访问,复用就有可被利用的漏洞
所以还是每次发放比较好