Generate random password
/** * Generate a random character array from the list of $possible characters * * @param array $possible Array of possible characters * @param int $length Length of string to generate * @return array An array of $length random characters */ function randomStringArray($possible, $length) { $stringArray = array(); $possibleCount = count($possible); $i = 0; while ($i < $length) { // pick a random character from the possible ones $char = $possible[mt_rand(0, $possibleCount - 1)]; // we don't want this character if it's already in the password if (!in_array($char, $stringArray)) { $stringArray[] = $char; $i++; } } return $stringArray; } /** * Generate password that contains 6 letters and 2 numbers */ function generatePassword() { $letters = array_merge(range('a', 'z'), range('A', 'Z')); $numbers = range('0', '9'); $chars = array_merge( randomStringArray($letters, 6), randomStringArray($numbers, 2)); shuffle($chars); return implode('', $chars); }
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home