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';
              $possible .= '!@#$%^&*()_+-=[]{};\':",./\\<>?|`~';
        $code = '';
        $i = 0;
        while ($i < $characters) {
         $code1 = substr($possible, mt_rand(0, strlen($possible)-1), 1);
         $code1= strtoupper($code1);
         $code .=$code1;

        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


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


Some examples of generated passwords are:

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.