php 生成随即密码

#-------------------------------------------
# 产生随机字串,可用来自动生成密码
# 默认长度6位 字母和数字混合
# $format ALL NUMBER CHAR 字串组成格式
#-------------------------------------------
 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;
 }
 mt_srand((double)microtime()*1000000*getmypid()); 
 $password="";
 while(strlen($password)<$len)
    $password.=substr($chars,(mt_rand()%strlen($chars)),1);
 return $password;
 } 

你可能感兴趣的:(PHP)