php 随机密码生成函数
php 随机密码生成函数
本函数使用
mt_srand 生成随机种子,密码的长度可以随意定义,最长 32 位。
代码如下
:
<?php
mt_srand((double) microtime() * 1000000);
function gen_random_password($password_length = 32, $generated_password = ""){
$valid_characters
=
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$chars_length = strlen($valid_characters) - 1;
for($i = $password_length; $i--; ) {
//$generated_password .= $valid_characters[mt_rand(0, $chars_length)];
$generated_password .= substr($valid_characters, (mt_rand()%(strlen($valid_characters))), 1);
}
return $generated_password;
}
?>
<!DOCTYPE
HTML
PUBLIC
"-//W3C//DTD
HTML
4.01
Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>php 密码生成器</title>
<style type="text/css">
body {
font-family: Arial;
font-size: 10pt;
}
</style>
</head>
<body>
<span style="font-weight: bold; font-size: 15pt;">密码生成器</span><br /><br />
<?php
if (isset()($_GET['password_length'])){
if(preg_match("/([0-9]{1,8})/", $_GET['password_length'])){
print("密码生成成功:<br />
<span style="font-weight: bold">" . gen_random_password($_GET['password_length']) .
"</span><br /><br />n");
} else {
print("密码长度不正确!<br /><br />n");
}