note 33705 added to function.crypt

From: Date: Fri, 04 Jul 2003 15:25:21 +0000
Subject: note 33705 added to function.crypt
Groups: php.notes 
Request: Send a blank email to php-notes+get-51515@lists.php.net to get a copy of this message
there is no passwordgenerator available in php, so i thought i would present mine in here.... Its just a simple function which you could change how you want it........ // function-call //$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=4; for(;;) { $pwd=null; $o=null; // Generates the password .... for ($x=0;$x<$digits;) { $y = rand(1,1000); if($y>350 && $y<601) $d=chr(rand(48,57)); if($y<351) $d=chr(rand(65,90)); if($y>600) $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; } $PASSWORD=generate_password(6,true,null); have fun ... ;-) ---- Manual Page -- http://www.php.net/manual/en/function.crypt.php Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+33705 Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+33705&report=yes Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+33705&report=yes

« previous php.notes (#51515) next »