如何实现用户id生成一个唯一邀请码

#如何实现用户id生成一个唯一邀请码
#创建验证码
function createCode($user_id) {

    static $source_string = 'E5FCDG3HQA4B1NOPIJ2RSTUV67MWX89KLYZ';

    $num = $user_id;

    $code = '';

    while ( $num > 0) {

        $mod = $num % 35;

        $num = ($num - $mod) / 35;

        $code = $source_string[$mod].$code;

    }

    if(empty($code[3]))

        $code = str_pad($code,4,'0',STR_PAD_LEFT);

    return $code;

}

#解密验证码
function decode($code) {

    static $source_string = 'E5FCDG3HQA4B1NOPIJ2RSTUV67MWX89KLYZ';

    if (strrpos($code, '0') !== false)

        $code = substr($code, strrpos($code, '0')+1);

    $len = strlen($code);

    $code = strrev($code);

    $num = 0;

    for ($i=0; $i < $len; $i++) {

        $num += strpos($source_string, $code[$i]) * pow(35, $i);

    }

    return $num;
}

#测试一下
$number = 18759;
$encode_code = createCode($number);
echo $encode_code;#0P4Z
echo '<br>';
$decode_code = decode($encode_code);
echo $decode_code;#18759

在不知道$source_string的情况下,是没办法解出正确的user_id的

猜你喜欢

转载自www.cnblogs.com/lovekingly/p/10294244.html
今日推荐