I am trying to generate a random password in php.
However I am getting all 'a's and the return type is of type array and I would like it to be a string. Any ideas on how to correct the code?
Thanks.
function randomPassword() {
$alphabet = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789";
for ($i = 0; $i < 8; $i++) {
$n = rand(0, count($alphabet)-1);
$pass[$i] = $alphabet[$n];
}
return $pass;
}
Your best bet is the RandomLib library by ircmaxell.
Usage example:
It produces strings which are more strongly random than the normal randomness functions like
shuffle()
andrand()
(which is what you generally want for sensitive information like passwords, salts and keys).Call it like in the comments.
Try this (use
strlen
instead ofcount
, becausecount
on a string is always1
):Demo: http://codepad.org/UL8k4aYK
Use this simple code for generate med-strong password 12 length