生成随机密码,验证码
PHP写法:
//A-Z a-z 0-9 !#$%^&*
$str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%^&*";
//生成随机字符
function createPwd($str,$len){
$pwd = '';
$strlen = strlen($str);
for($i=0;$i<$len;$i++){
$pwd .= $str{mt_rand(0,$strlen-1)};
}
return $pwd;
}
echo createPwd($str,16);
js写法
//A-Z a-z 0-9 !#$%^&*
var str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%^&*";
//生成随机字符
function createPwd(str,len){
var pwd = '';
var maxPos = str.length;
for(var i=0;i<len;i++){
pwd += str.charAt(Math.floor(Math.random()*maxPos));
}
return pwd;
}
var passwd = createPwd(str,16);
document.write(passwd);
还没有评论,来说两句吧...