generating diffrent passwords

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
pelegk2
Forum Regular
Posts: 633
Joined: Thu Nov 27, 2003 5:02 am
Location: Israel - the best place to live in after heaven
Contact:

generating diffrent passwords

Post 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
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post 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.
Last edited by qads on Fri Jun 11, 2004 3:36 am, edited 1 time in total.
fastfingertips
Forum Contributor
Posts: 242
Joined: Sun Dec 28, 2003 1:40 am
Contact:

Post 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.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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);
:o
Post Reply