public function makeToken($string, $expiry = 0) {
$key = random(10);
$token = random(16);
$unique = md5($string);
$password = authcode($string, 'ENCODE', $key, $expiry);
$data = array();
$data['salt'] = $key;
$data['click'] = $token;
$data['password'] = $password;
$data['expiry'] = $expiry;
$data['unique'] = $unique;
$data['addtime'] = time();
$db = get_db('token', 'un_');
$result = $db->where(array('unique' => $unique, 'status' => 0))->find();
if(empty($result)) {
$db->add($data);
} else {
$token = $result['click'];
}
return $token;
}
public function parseToken($token) {
$db = get_db('token', 'un_');
$result = $db->where(array('click'=>$token))->find();
if(!empty($result)) {
$key = $result['salt'];
$password = $result['password'];
$expiry = $result['expiry'];
$data = authcode($password, 'DECODE', $key, $expiry);
if(empty($data)) {
$db->where(array('click'=>$token))->delete();
return false;
}
return $data;
}
return false;
}
public function testmt() {
$username = 'tianlunvip';
$id = 0;
$topic = 91;
$array = array($username, $id, $topic);
$string = implode(" ", $array);
echo $this->makeToken($string);
}
public function testpt() {
$token = I('s', '');
$data = $this->parseToken($token);
if($data !== false) {
$arr = explode(" ", $data);
list($username, $id, $topic) = $arr;
}
echo $username.$id.$topic;
}
口令生成与解析
相关推荐
评论
暂无评论...