CrankBerry Blog Title
2009


(1) Comment

Random code and password generator in PHP

Sometimes when you're interacting with users you need to generate a random string of code or password. For example, a user is signing up for a service and you decided to give them some random password to start with, that's what I'm talking about. Here I have a quick script which allows you to generate random code with a defined length.

Random Code and Password Generator

Here it is, a function you can use to plug right into your script:

//Random Code and Password Generator  by Jerry Low @crankberryblog.com
//Find other useful scripts at the Crankberry Blog

//This is the simple code Generator
function simple_code($code_len = 1) {
        //Starting The Code
        $code = "";
       
        for ($i=1; $i<=$code_len; $i++) {
                //Keep Generating Characters Until its a proper character
                do {
                        //Generate the Characters
                        $code_char = rand(48, 122);
                } while (($code_char >= 58 && $code_char<=64) || ($code_char >= 91 && $code_char<=96));
               
                //Add Character to Code
                $code .= chr($code_char);
               
        }
       
        return $code;
}

Just call the function from one of either ways. They're both calling a code of 5 characters right now but that's the number you can adjust yourself.

$code = simple_code(5);
$code = $object->simple_code(5);

Random Code and Password Generator with Configuration

So the above script allows you to generate codes with random characters including numbers, capital letters and lower case letters. It may not be for everyone so here we'll add some adjustments where you can tweak it. Here's the code:

//Random Code and Password Generator  by Jerry Low @crankberryblog.com
//Find other useful scripts at the Crankberry Blog

//This is the simple code Generator
function simple_code($code_len = 1, $have_num = TRUE, $have_caps = TRUE, $have_lower = TRUE) {
        if (!$have_num && !$have_caps && !$have_lower) {
                return 'Please Have at Least One Setting On';
        } else {
                //Starting The Code
                $code = "";
               
                for ($i=1; $i<=$code_len; $i++) {
                        //Keep Generating Characters Until its a proper character
                        do {
                                //Is The Code Good
                                $code_ok = TRUE;
                               
                                //Generate the Characters
                                $code_char = rand(48, 122);
                               
                                //Which Codes do they want
                                if (!$have_num && ($code_char>=48 && $code_char<=57)) $code_ok = FALSE;
                                if (!$have_caps && ($code_char>=65 && $code_char<=90)) $code_ok = FALSE;
                                if (!$have_lower && ($code_char>=97 && $code_char<=122)) $code_ok = FALSE;
                               
                                //Bad Chars
                                if (($code_char >= 58 && $code_char<=64) || ($code_char >= 91 && $code_char<=96)) $code_ok = FALSE;
                        } while (!$code_ok);
                       
                        //Add Character to Code
                        $code .= chr($code_char);
                       
                }
               
                return $code;
        }
}

Just call the function like the previous one but configuring the settings like so:

$code = simple_code(5, TRUE, FALSE, TRUE);
$code = $object->simple_code(5, TRUE, FALSE, TRUE);

The arguements after the length of the code is as follow:

  1. Have numbers in the code
  2. Have capital letters in the code
  3. Have lower case letters in the code

Here are some examples of different configurations for you to understand how it works.

$code = simple_code(5, TRUE, FALSE, FALSE); //Numbers only
$code = simple_code(5, FALSE, TRUE, TRUE); //Letters only
$code = simple_code(5, TRUE, TRUE, FALSE); //Numbers and capital letters only
TL
This entry was posted on Sunday, September 27th, 2009 at 9:53 am 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.
BL

One Response to “Random code and password generator in PHP”

  1. [...] short it maybe too easy for bots; thus, five is an acceptable length. The code above integrates a random code generator which I have demonstrated [...]

Leave a Reply

Spam protection by WP Captcha-Free