Version: 2.0
Type: Function
Category: Algorithms
License: GNU General Public License
Description: Very simple script which generates an 8 digit paswoord cinsisting of letters and numbers and capital letters
/* Author: J.B.Lamer, Date: 20070321 * * generates a password of length between $minlen and $maxlen * This calls getPossibleString() (below) that returns a string of possible * characters for the password * if $maxlen is null (or out of range) than $maxlen will equal minlen * if $minlen is out of range than it will equal 8 (the range is between 4 and 20) * $use_upper, $str_include, and $str_exclude are used to build possible string * see getPossibleString() * * a good way to call is $p = generate_password(6,10,true,'','0OQI1lqg'); * so you exclude zero,'oh', and Q; capital i, one, and lower L; q and g; and whatever * else you think look the same * That will generate a password of length 6 to 10 that have capital or lower case letters */ function generate_password( $minlen=8, $maxlen=null, $use_upper=false, $str_include=null, $str_exclude=null ) { if ( !(($minlen = intval($minlen)) >= 4 && $minlen <= 20) ) { $minlen = 8; } if ( is_null($maxlen) || !(($maxlen = intval($maxlen)) >= 4 && $maxlen <= 20) ) { $maxlen = $minlen; } $password = ''; $possible = getPossibleString( $use_upper, $str_include, $str_exclude ); $p_len = strlen($possible); // care about repeating? make proper checks if you do $len = mt_rand($minlen,$maxlen); for ( $i=0; $i < $len; $i++ ) { $password .= substr( $possible, mt_rand(0, $p_len), 1 ); } return $password; } /* Author: J.B.Lamer, Date: 20070321 * * generate a string of all possible chars that can be used for password * automatically, there is 0-9 and a-z * $use_upper set to true includes A-Z * $str_include is a string of any extra characters to include * $str_exclude is a string of any characters to exclude.. this is called last */ function getPossibleString( $use_upper=false, $str_include=null, $str_exclude=null ) { $possible = ''; for ( $i=ord('0'); $i <= ord('9'); $i++ ) { $possible .= chr($i); } for ( $i=ord('a'); $i <= ord('z'); $i++ ) { $possible .= chr($i); } for ( $i=ord('A'); $use_upper && $i <= ord('Z'); $i++ ) { $possible .= chr($i); } if ( !is_null( $str_include ) && strlen($str_include) > 0 ) { $str_include = strval($str_include); for ( $i=0; $i < strlen($str_include); $i++ ) { if ( false === strpos( $possible, $str_include[$i] ) ) { $possible .= $str_include[$i]; } } } if ( !is_null( $str_exclude ) && strlen($str_exclude) > 0 ) { $str_exclude = strval($str_exclude); for ( $i=0; $i < strlen($str_exclude); $i++ ) { if ( false !== ($pos = strpos($possible, $str_exclude[$i])) ) { $hold = ''; $hold .= substr($possible,0,$pos); if ( $pos+1 < strlen($possible) ) { $hold .= substr($possible,$pos+1); } $possible = $hold; } } } return $possible; }