Home >Backend Development >PHP Tutorial >PHP generates random password (php custom function)
A function that generates a random password. The password is a random string of lowercase letters and numbers. The length can be customized.
Copy the code as follows:
/*
* PHP automatically generates a new password custom function (with example demonstration)
Applicable environment: PHP5.2.x / mysql 5.0.x
Code author: xujiajay
Contact: xujiaphp@gmail.com
* */
function genPassword($min = 5, $max = 8)
{
$validchars="abcdefghijklmnopqrstuvwxyz123456789";
$max_char=strlen($val idchars)-1;
$length=mt_rand($min,$max);
$password = "";
for($i=0;$i {
$password.=$validchars[mt_rand(0, $max_char)];
}
} return $password;
}
echo "New password: ".genPassword()."
";
echo "New password: ".genPassword(5,10)."
";
?>