rand() 生成32位随机码(code)
rand() 返回 0 到 RAND_MAX 之间的伪随机整数 , 比如你想要 10 到 20(包括 10 和 120)之间的随机数,用 rand(10, 20) 即可 . 如下是利用 rand()函数生成一个32位随机码(code) , 其中还利用到了shuffle()函数 ; shuffle()函数是将数组打乱(随机排列单元的顺序) . 要注意的是shuffle()函数为 array 中的单元赋予新的键名。这不仅是重新排序而且将删除原有的键名。
$code = "";
$chars = array(
"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", "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", "0", "1", "2",
"3", "4", "5", "6", "7", "8", "9"
);
$chars_len = count($chars) - 2;
shuffle($chars);
$length = 32;
for($i=0; $i<$length; $i++){
$rander = rand(0, $chars_len);
$code .= $chars[$rander];
}
echo $code;
- 添加新评论
- 485 次点击