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;
}
Tiny code with 2 line.
demo: http://codepad.org/5rHMHwnH
I created a more comprehensive and secure password script. This will create a combination of two uppercase, two lowercase, two numbers and two special characters. Total 8 characters.
I know you are trying to generate your password in a specific way, but you might want to look at this method as well...
It's taken from the php.net site and it creates a string which is twice the length of the number you put in the openssl_random_pseudo_bytes function. So the above would create a password 4 characters long.
In short...
Would create a password 8 characters long.
Note however that the password only contains numbers 0-9 and small cap letters a-f!
In one line:
Try This with Capital Letters, Small Letters, Numeric(s) and Special Characters
Example Output(s) :
,IZCQ_IV\7
@wlqsfhT(d
1!8+1\4@uD