Another powerful password generator using php

Post Reply
User avatar
Saman
Lieutenant Colonel
Lieutenant Colonel
Posts: 828
Joined: Fri Jul 31, 2009 10:32 pm
Location: Mount Lavinia

Another powerful password generator using php

Post by Saman » Fri Jan 06, 2012 6:13 pm

Here is another nice version of a php password generator. You can also have a look at the previous version I posted How to create a random password using php.

Code: Select all

/* generate a new password & return the new password, or false on failure */
/*  $digits = amount of chars the password should have (between 4 and 29) */
/*  $c = if true, I,i,L,l will be changed to 1 and O or o will be changed to 0 (Zero) to prevent mistakes by an userinput */
/*  $st = string to "U" = upper, "L" = lower, NULL=casesensitive */
function generate_password($digits,$c,$st)
{
  if(!ereg("^([4-9]|((1|2){1}[0-9]{1}))$",$digits)) // 4-29 chars allowed
     $digits=8;
  for(;;)
  {
     $pwd=null; $o=null;
     // Generates the password ....
     for ($x=0;$x350 && $y600) $d=chr(rand(97,122));
        if($d!=$o)
        { 
           $o=$d; $pwd.=$d; $x++;
        }
     }
     // if you want that the user will not be confused by O or 0 ("Oh" or "Null")
     // or 1 or l ("One" or "L"), set $c=true;
     if($c)
     {
        $pwd=eregi_replace("(l|i)","1",$pwd);
        $pwd=eregi_replace("(o)","0",$pwd);
     }
     // If the PW fits your purpose (e.g. this regexpression) return it, else make a new one
     // (You can change this regular-expression how you want ....)
     if(ereg("^[a-zA-Z]{1}([a-zA-Z]+[0-9][a-zA-Z]+)+",$pwd))
        break; 
  }
  if($st=="L") $pwd=strtolower($pwd);
  if($st=="U") $pwd=strtoupper($pwd);
  return $pwd;
}
 
Post Reply

Return to “PHP & MySQL”