Php random password generator

March 17, 2009    password generator php random web development

I developed this the other day and think its a really useful bit of code to use if your looking to make yourself a strong password generator for your users.
[sourcecode language='php']
function generatePassword($length=9, $strength=0) {
$vowels = 'aeuy';
$consonants = 'bdghjmnpqrstvzuacd';
$consonants .= 'BDGHJLMNPQRSTVWXZUACD';
$vowels .= "AEUY";
$consonants .= '23456789';
$consonants .= '@#$%!';

$password = '';
$alt = time() % 2;
for ($i = 0; $i < $length; $i++) {
if ($alt == 1) {
$password .= $consonants[(rand() % strlen($consonants))];
$alt = 0;
} else {
$password .= $vowels[(rand() % strlen($vowels))];
$alt = 1;
}
}
return $password;
}

[/sourcecode]

The function is called in the normal way something like below:

[sourcecode language='php']
$strength = 8;
$password = generatePassword($length,$strength);
[/sourcecode]

An example result of this password generator is below:

Your password: REWEMYAuHy$e

Enjoy!


blog comments powered by Disqus