עוד אחד
random_password_v1 <?php /********************************************************************* * random_password_v1.php * * Author: Steve Werby <steve at(@) befriend dot(.) com> * Created: 2000-10-19 19:55 * Revised: 2000-12-19 20:28 * * Purpose: Create a random password. * * Latest version always available at http://www.befriend.com/. *********************************************************************/ // Generates a random password. function random_password_v1( $characters_allow = 'a-z,A-Z,1-9', $characters_disallow = 'i,o', $password_length = 8, $repeat = 0 ) { // Generate array of allowable characters. $characters_allow = explode( ',', $characters_allow ); for ( $i = 0; $i < count( $characters_allow ); $i++ ) { if ( substr_count( $characters_allow[$i], '-' ) > 0 ) { $character_range = explode( '-', $characters_allow[$i] ); for ( $j = ord( $character_range[0] ); $j <= ord( $character_range[1] ); $j++ ) { $array_allow[] = chr( $j ); } } else { $array_allow[] = $characters_allow[$i]; } } // Generate array of disallowed characters. $characters_disallow = explode( ',', $characters_disallow ); for ( $i = 0; $i < count( $characters_disallow ); $i++ ) { if ( substr_count( $characters_disallow[$i], '-' ) > 0 ) { $character_range = explode( '-', $characters_disallow[$i] ); for ( $j = ord( $character_range[0] ); $j <= ord( $character_range[1] ); $j++ ) { $array_disallow[] = chr( $j ); } } else { $array_disallow[] = $characters_disallow[$i]; } } mt_srand( ( double ) microtime() * 1000000 ); // Generate array of allowed characters by removing disallowed // characters from array. $array_allow = array_diff( $array_allow, $array_disallow ); // Resets the keys since they won't be consecutive after // removing the disallowed characters. reset( $array_allow ); $new_key = 0; while( list( $key, $val ) = each( $array_allow ) ) { $array_allow_tmp[$new_key] = $val; $new_key++; } $array_allow = $array_allow_tmp; while ( strlen( $password ) < $password_length ) { $character = mt_rand( 0, count( $array_allow ) - 1 ); // If characters are not allowed to repeat, // only add character if not found in partial password string. if ( $repeat == 0 ) { if ( substr_count( $password, $array_allow[$character] ) == 0 ) { $password .= $array_allow[$character]; } } else { $password .= $array_allow[$character]; } } return $password; } /********************************************************************* * Usage of random_password_v1(). *********************************************************************/ echo random_password_v1('A-P,d-z,1-9','O,3-6',10,0); ?>