faqts : Computers : Programming : Languages : PHP : Common Problems : Tips and Tricks

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

22 of 31 people (71%) answered Yes
Recently 4 of 10 people (40%) answered Yes

Entry

How can I generate a random alphanumeric password?

Aug 12th, 1999 22:29
Nathan Wallace, Brian Schaffner


Try this:

    $myPass = substr(ereg_replace("[^A-Za-z]", "", crypt(time())) .
                         ereg_replace("[^A-Za-z]", "", crypt(time())) .
                         ereg_replace("[^A-Za-z]", "", crypt(time())),
                     0, 6);

the three crypts should ensure that you get enough Alpha characters.
crypt() by itself returns 13 "printable ASCII characters", the first 2
being the salt which is in the set [a-zA-Z0-9./]