$len表示長度,代碼如下:
復制代碼 代碼如下:
/**
* 產生隨機字符串
*
* 產生一個指定長度的隨機字符串,並返回給用戶
*
* @access public
* @param int $len 產生字符串的位數
* @return string
*/
function randstr($len=6) {
$chars='ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz0123456789-@#~';
// characters to build the password from
mt_srand((double)microtime()*1000000*getmypid());
// seed the random number generater (must be done)
$password='';
while(strlen($password)<$len)
$password.=substr($chars,(mt_rand()%strlen($chars)),1);
return $password;
}