来自《PHP核心技术与最佳实战》
<?php
define('SECRET', '67%$#ap28');
function m_token()
{
$str = mt_rand(1000, 9999);
$tim = $_SERVER['REQUEST_TIME'] - $str;
// 十进制转换为十六进制
$str2 = dechex($tim);
$sub_str = substr(md5($str . SECRET), 0, 10);
$result = $str . $sub_str . $str2;
return $result;
}
$token = m_token();
function v_token($str, $delay = 1)
{
$rs = substr($str, 0, 4);
$middle = substr($str, 0, 14);
$rs2 = substr($str, 14, 8);
// $resu = ($middle == $rs . substr(md5($rs . SECRET), 0, 10)) && ($_SERVER['REQUEST_TIME'] - hexdec($rs2) - $rs < $delay);
$resu = ($middle == $rs . substr(md5($rs . SECRET), 0, 10));
return $resu;
}