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:
//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 = $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:
//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 = $object->simple_code(5, TRUE, FALSE, TRUE);
The arguements after the length of the code is as follow:
- Have numbers in the code
- Have capital letters in the code
- 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, FALSE, TRUE, TRUE); //Letters only
$code = simple_code(5, TRUE, TRUE, FALSE); //Numbers and capital letters only













[...] 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 [...]