Saturday 10 April 2010

How to generate random passwords for PHP users.

Hey guys.

I have a script that I'd like to share and it is about generating random password. You can very well use this script for your new user registration. And then mail it to them.

This is useful when people create accounts and have easy to remember passwords but passwords shouldn't be easy to guess. Therefore, this script is going to output something that will be hard for user to guess.

Here it is:

function GeneratePassword($min = 5, $max = 8) {
$ValidChars = "abcdefghijklmnopqrstuvwxyz123456789";
$max_char = strlen($ValidChars) - 1;
$length = mt_rand($min, $max);
$password = "";
for ($i = 0; $i < $length; $i++) {
$password .= $ValidChars[mt_rand(0, $max_char)];
}
return $password;
}
echo "New Password = " . GeneratePassword()
. "\n";
echo "New Password = " . GeneratePassword(4, 10) . "\n";




Now all you have to do is call this function and you are there!

Enjoy!

No comments :