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 密码生成器 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>