口令生成与解析

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;
}
赞(0) 打赏
取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

    暂无评论...