generate random password
Posted: Sat Apr 29, 2006 8:23 pm
Hello everybody!
How do I generate a random password?
Thanks,
Rob.
How do I generate a random password?
Thanks,
Rob.
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
http://www.php.net/manual/en/function.mt-rand.php -- Random FunctionDobson wrote:Hello everybody!
How do I generate a random password?
Code: Select all
$password = mt_rand();Code: Select all
$chars = 'abcdefghijklmnopqrstuvwxyz1234567890';
$password = '';
for($i=0; $i <= 15; $i++)
{
$char = mt_rand(0, strlen($chars)-1);
$password .= $chars{$char};
}
echo $password;Code: Select all
$rand=rand(5,25);
$new="";
for ($i=0;$i<$rand;$i++)
{
$case=rand(0,1);
if ($case=='0')
{
$letter=rand(65,90);
$add=chr($letter);
// uppercase letters
$new=$new.$add;
}
else
{
$letter=rand(97,122);
$add=chr($letter);
//lowercase letters
$new=$new.$add;
}
}
$password=md5($new);Code: Select all
/*
RETURN PASSWORD OF X AMOUNT OF LENGTH
*/
function getDefaultPassword( $pLen = 7 ){
if( ! ( $pLen > 0 && $pLen < 20 ) )
$pLen = 7;
$str = strrev(substr(md5(microtime()),0,$pLen));
if(!isSafeWord($str))
getDefaultPassword();
else
return $str;
}Code: Select all
function GenerateRandomString($minlength, $maxlength, $uselower, $useupper, $usespecial, $usenumbers) {
$charset = null;
$key = null;
if ($uselower) $charset .= "abcdefghijklmnopqrstuvwxyz";
if ($useupper) $charset .= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
if ($usespecial) $charset .= "~@#$%^*()_+-={}|]["; // Note: using all special characters this reads: "~!@#$%^&*()_+`-={}|\\]?[\":;'><,./";
if ($usenumbers) $charset .= "0123456789";
if ($minlength > $maxlength) $length = mt_rand ($maxlength, $minlength);
else $length = mt_rand ($minlength, $maxlength);
for ($i=0; $i<$length; $i++) $key .= $charset[(mt_rand(0,(strlen($charset)-1)))];
return $key;
}This is a random string which could (Very small chance) provide a word that has a curse or offensive word. The isSafeWord() functions just validates that there are no words from a list in the random string.dibyendrah wrote:what does the isSafeWord() functions does ?