function generate_password( $length = 8 ) { // 密码字符集,可任意添加你需要的字符 $chars = ‘abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_ []{}<>~`+=,.;:/?|'; $password = ”; for ( $i = 0; $i < $length; $i++ ) { // 这里提供两种字符获取方式 // 第一种是使用 substr 截取$chars中的任意一位字符; // 第二种是取字符数组 $chars 的任意元素 // $password .= substr($chars, mt_rand(0, strlen($chars) – 1), 1); $password .= $chars[ mt_rand(0, strlen($chars) - 1) ]; } return $password; }
ps:md5加密和生成32位的字符串
mt_rand 生成指定范围的随机数
time 可获得时间戳
用随机数 + 当前时间戳可以获得较不会重复的随机数
$rand = md5(time() . mt_rand(0,1000));