php生成随机字符串


1:
function getPassword($min = 6, $max = 20)
{
$validchars=”abcdefghijklmnopqrstuvwxyz123456789″;
$max_char=strlen($validchars)-1;
$length=mt_rand($min,$max);
//返回$min到$max的伪随机数,mt_rand()产生随机数值的平均速度比 libc 提供的 rand() 快四倍
$password = “”;
for($i=0;$i {
$password.=$validchars[mt_rand(0,$max_char)];
}
return $password;
}

2:
function makePass($length)
{
$possible = “0123456789!@#$%^&*()_+”.
“abcdefghijklmnopqrstuvwxyz”.
“ABCDEFGHIJKLMNOPQRSTUVWXYZ”;
$str = “”;
while(strlen($str) < $length)
{
$str .= substr($possible, (rand() % strlen($possible)), 1);
}
return($str);
}

3:
function randStr($len=6,$format=’ALL’) {
switch($format) {
case ‘ALL’:
$chars=’ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-@#~’; break;
case ‘CHAR’:
$chars=’ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-@#~’; break;
case ‘NUMBER’:
$chars=’0123456789′; break;
default :
$chars=’ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-@#~’;
break;
}
//microtime()取得目前时间的UNIX 时间戳记的百万分之一秒值
mt_srand((double)microtime()*1000000*getmypid());
$password=””;
while(strlen($password) $password.=substr($chars,(mt_rand()%strlen($chars)),1);
}
return $password;
}

发表评论

3 + 9 = ?