隨機密碼生成也是常用的方法,使用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 密碼生成器 v 4.0</title>
<style type="text/css">
body {
font-family: Arial;
font-size: 10pt;
}
</style>
</head>
<body>
<span style="font-weight: bold; font-size: 15pt;">密碼生成器v4.0 by freemouse</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");
}
}
print <<< end
請為密碼生成其指定生成密碼的長度:<br /><br />
<form action="{$_SERVER['PHP_SELF']}" method="get">
<input type="text" name="password_length">
<input type="submit" value="生成">
</form>
end;
?>
</body>
</html>