A random Password Generator script - Full featured

Here is random Password generator function, it can produce complex passwords of any desired length. It can generate passwords which are alpha-numeric + with special characters (optional) + upper and lower case mixed within it.

 function generateCode($characters,$special_char=false) {
        $possible = '1234567890abcdefghijklmnopqrstuvwxyz';
            if($special_char)
              $possible .= '!@#$%^&*()_+-=[]{};\':",./\\<>?|`~';
        $code = '';
        $i = 0;
        while ($i < $characters) {
         $code1 = substr($possible, mt_rand(0, strlen($possible)-1), 1);
         if(rand()%2==0)
         $code1= strtoupper($code1);
         $code .=$code1;
         $i++;
         }

        return $code;
 }
 

Features of this function:

  • Alpha-numeric passwords
  • You can get random password of any desired length
  • Can produce password of combined upper and lowercase characters.
  • Can optionally produce passwords with special characters

Suppose you want to produce password of length 8 characters, then the syntax to call the function is

generateCode(8);
 

If you want to create password with special characters also, then the syntax will be

generateCode(8,true);
 

Some examples of generated passwords are:
Mm69lIGf
“)5E%\{7
M4p3OVEod6Dq9cJZMkGZMEnm31JVGcNY8iK
J4j:8~-72)^fF>=6)S<(”{~->a0gQ6(DrEk



If you don’t need a complex passwords generator like this then you can try A simple Password Generator here it is one-line code which can generate simple random passwords.

Last Modified: February 28, 2008 @ 21:16

This entry was posted on Thursday, February 28th, 2008 at 8:35 PM and is filed under PHP. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

2 Responses to “A random Password Generator script - Full featured”

  1. café tarif euforie Said on

    I appreciate, cause I found exactly what I was looking for. You have ended my four day long hunt! God Bless you man. Have a great day. Bye

  2. Nenas Lindas Said on

    Me sorry for my english not so well, but me think that you are write too pointe.

Leave a Reply

You must be logged in to post a comment.