Home  >  Article  >  php教程  >  PHP随机密码生成

PHP随机密码生成

WBOY
WBOYOriginal
2016-06-13 10:11:091121browse

产生随机字串,可用来自动生成密码。
特点:
1. 可以指定密码包含数字或字符,默认为混和模式
2. 指定随意密码长度,默认长度为6位

代码如下:
#-------------------------------------------
# 产生随机字串,可用来自动生成密码
# 默认长度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) $password.=substr($chars,(mt_rand()%strlen($chars)),1);
return $password;
}

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn