php-根据user_id获得唯一验证码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

* 根据user_id得到唯一验证码算法
* 算法简介:设定所需参数共36个,使用user_id%36,得到第一位,使用user_id/36得到第二次商的算法,为了保证得到4位以上的邀请码,$user_id+46656
* @param $userId
* @return string
*/
static private function ($userId)
{
$sourceString = [
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l',
'm', 'n', 'o', 'p', 'q', 'r',
's', 't', 'u', 'v', 'w', 'x',
'y', 'z'
];
$num = $userId + 46656;
$code = '';
while ($num) {
$mod = $num % 36;
$num = (int)($num / 36);
$code = $code . $sourceString[$mod];
}
return $code;
}