获取随机码,支持纯数字和数字字母组合
第一种
function randomkeys($length, $type = 'number', $zimu_num = 0)
{
$pattern = '1234567890abcdefghijklmnopqrstuvwxyz';
$key='';
$temp = $length - $zimu_num;
$i = 0;
while ($i < $length) {
if ($type == 'number') {
$key .= $pattern[mt_rand(0, 9)];
} else {
if ($zimu_num == 0) {
$key .= $pattern[mt_rand(0, 35)];
} else {
if ($i < $temp) {
$key .= $pattern[mt_rand(0, 9)];
}
}
}
++$i;
}
$j = 0;
while ($j < $zimu_num) {
$temp_key = $pattern[mt_rand(10, 35)];
$key = rand_in_str($key, $temp_key);
++$j;
}
return $key;
}
第二种
function rand_str($length, $numeric = 0)
{
mt_srand((double) microtime() * 1000000);
if ($numeric) {
$hash = sprintf('%0' . $length . 'd', mt_rand(0, pow(10, $length) - 1));
} else {
$hash = '';
$chars = 'Aa1Bb2Cc3Dd4Ee5Ff6Gg7Hh8Ii9JjKkLlMmNnOPpQqRrSsTtUuVvWwXxYyZz';
$max = strlen($chars) - 1;
for ($i = 0; $i < $length; $i ++) {
$hash .= $chars[mt_rand(0, $max)];
}
}
return $hash;
}