Page 1 of 1
generating diffrent passwords
Posted: Thu Jun 10, 2004 3:38 pm
by pelegk2
i want to creaqte and 8 letter
password which will be diffrent from user to iser
what is the best way to do it
so even if i will generate 8 letters/digits by my self
i want to prevent a case where two users have same password
how do i do that?
thnaks in advance
peleg
Posted: Thu Jun 10, 2004 3:55 pm
by pickle
Well, you can create a random string, then query the db to see if it currently exists. If it does, regenerate until you generate a new password.
Posted: Thu Jun 10, 2004 4:57 pm
by qads
a better way to do it useing pickle's suggestion...
Code: Select all
<?php
$string = password();//a function which returns 5 letters/numbers/mixed value
$check = mysql_num_rows(mysql_query("select password from table where password = '$string'"));
if($check > 0)
{
$rand = rand(10, 32);
$md = md5($string.time());
$string .= $md{$rand}.($check + 1);
}
?>
this way, you dont need to loop to regenerate.
Posted: Fri Jun 11, 2004 3:22 am
by fastfingertips
hmm
Code: Select all
<?php
function makeRand ($minlength, $maxlength, $useupper, $usespecial, $usenumbers)
{ //Random string generator
$key="";
$charset = "abcdefghijklmnopqrstuvwxyz";
if ($useupper) $charset .= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
if ($usenumbers) $charset .= "0123456789";
if ($usespecial) $charset .= "~@#$%^*()_+-={}|][";
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;
}
do
{
$tempPass="";
$tempPass=$this->utils->makeRand(15,15,1,0,1);
} while(in_array($tempPass,$arrPass));
//$arrPass is a array of present passwords
?>
If you will plan to have more then 1000 users you should let MySQL server to make the comparion and he will return you a bool and if is founded (true) make again otherwise write it in db.
Posted: Fri Jun 11, 2004 3:26 am
by markl999
I'd just use:
Code: Select all
require_once 'Text/Password.php'; //see http://pear.php.net/package/Text_Password
$password = Text_Password::create(8);
