Random readable password
Generates a random readable password by combining words from a wordlist array.
You can adjust the password length trough a parameter (length).
function random_readable_pwd($length=10){ // the wordlist from which the password gets generated // (change them as you like) $words = 'dog,cat,sheep,sun,sky,red,ball,happy,ice,'; $words .= 'green,blue,music,movies,radio,green,turbo,'; $words .= 'mouse,computer,paper,water,fire,storm,chicken,'; $words .= 'boot,freedom,white,nice,player,small,eyes,'; $words .= 'path,kid,box,black,flower,ping,pong,smile,'; $words .= 'coffee,colors,rainbow,plus,king,tv,ring'; // Split by ",": $words = explode(',', $words); if (count($words) == 0){ die('Wordlist is empty!'); } // Add words while password is smaller than the given length $pwd = ''; while (strlen($pwd) < $length){ $r = mt_rand(0, count($words)-1); $pwd .= $words[$r]; } // append a number at the end if length > 2 and // reduce the password size to $length $num = mt_rand(1, 99); if ($length > 2){ $pwd = substr($pwd,0,$length-strlen($num)).$num; } else { $pwd = substr($pwd, 0, $length); } return $pwd; }
Snippet Details
-
AuthorJonas John
-
LicensePublic Domain
-
LanguagePHP
-
Created04/25/2006
-
Updated03/11/2011
-
Tagsstring functions, random
random_readable_pwd(10) => returns something like: pingwater6, radiohap28, sunwhite84, happykid44, etc...
Sorry folks, comments have been deactivated for now due to the large amount of spam.
Please try to post your questions or problems on a related programming board, a suitable mailing list, a programming chat-room,
or use a QA website like stackoverflow because I'm usually too busy to answer any mails related
to my code snippets. Therefore please just mail me if you found a serious bug... Thank you!
Older comments:
Jonas March 11, 2011 at 20:16
I fixed the snippet - Thanks Jamie!
Jamie Bicknell September 22, 2008 at 16:20
Do you not need an extra comma at the end of each $word variable. Otherwise, the last one and the first one join together. Eg: icegreen and turbomouse
$words = 'dog,cat,sheep,sun,sky,red,ball,happy,ice';
$words .= 'green,blue,music,movies,radio,green,turbo';
to
$words = 'dog,cat,sheep,sun,sky,red,ball,happy,ice,';
$words .= 'green,blue,music,movies,radio,green,turbo,';
$words = 'dog,cat,sheep,sun,sky,red,ball,happy,ice';
$words .= 'green,blue,music,movies,radio,green,turbo';
to
$words = 'dog,cat,sheep,sun,sky,red,ball,happy,ice,';
$words .= 'green,blue,music,movies,radio,green,turbo,';