/ Published in: PHP
This function generates a random password of a specified length in lower or mixed case letters and numbers. Ambiguous characters, such as I, L, 1, 0, and O, are left out so the user can easily read the password.
Expand |
Embed | Plain Text
function random_password($len = 7, $mixed_case = false) { $a = "abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789"; $out = ""; for($i = 0; $i < $len; $i++) return $out; }
Comments
Subscribe to comments
You need to login to post a comment.

apparently mt_rand() is up to 4x faster
Hmmm... I doubt performance was what was needed anyways.
Precalculating strlen($a) does make a noticable difference, otherwise, a 5 million loop run gives mt_rand() 17.07s and rand() 17.19s on my mac... sure, we're not in 2008 anymore ^^
I found an odd error when testing 20 character long mixed case passwords. Most of the time it works, but every once in a while it will only return 19 characters, and give an error saying Notice: Uninitialized string offset: 54 in C:\filepath\index.php on line " $out .= $a[rand(0, strlen($a))];". What would cause the index to go out of the range of the string?
The error is due to the rand function call. The function strlen returns the length of the string, but if you use the length in the rand function, which has an inclusive range, then sometimes you will get a reference to an index that is one above the max index, because the index starts at 0.