...

How To Generate Random Passwords With PHP

makintyre
10-06-2011, 03:05 PM
$length =10;
$characters_to_use ="abcdef1234567890";

for($i = 0; $i < $length; $i++)
{
$do = mt_rand(0,strlen($characters_to_use)-1);
$password = $password . $characters_to_use{$do};
}

echo $password;

jmj001
01-27-2012, 04:52 AM
IMHO the characters 1 and 0 should be left out as they can be confused with l and O...

This is my passwdgenmachinefunctionthingymajig...

function create_random_password($len=7){
$chars = "abcdefghijkmnpqrstuvwxyz23456789";
srand((double)microtime()*1000000);
$i = 0;
$pass = '' ;
while ($i <= $len) {
$num = rand() % 33;
$tmp = substr($chars, $num, 1);
$pass = $pass . $tmp;
$i++;
}
return $pass;
}

kbluhm
01-27-2012, 02:18 PM
Since years and years ago there is no longer a need to seed the random number generator with srand(). ;)

Also, mt_rand() is much faster than rand().

jmj001
01-27-2012, 04:10 PM
I must admit, I've been using that function for close to 10 years... so it's probably well outdated.

I still however recommend taking out the 1 & 0 so people don't confuse them .. just saying ;)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum