PHP Random code Generator

This is a very simple-to-use php function that help you generate random codes in very easy way, you will need this function to generate random passwords, confirmation links,...

So, this is it:
function createRandomPassword() {
    $chars = "abcdefghijkmnopqrstuvwxyz023456789";
    $i = 0;
    $pass = '' ;
    while ($i <= 9) {
        $num = rand() % 33;
        $tmp = substr($chars, $num, 1);
        $pass = $pass . $tmp;
    return $pass;

That's it!
There are many options you can play with, for example you can choose the characters to generate code from: $chars ="1234567890" will generate numeric code; $chars = "abcdefghijkmnopqrstuvwxyz023456789"; will generate alphanumeric codes. In fact the code will be generated randomly from the list of characters in $chars.

You can also change the lenght of the generated code by changing the value in "while ($i <= 9)", 9 here means you will get a code with 10 characters, as this counter (while) starts from 0 to 9. If you do "while ($i <= 7)" you will get 8 characters code.

This is the easiest and the best way to generate random code from php. I will always provide the best PHP scrips :)

If you like my blog, please give a link to us from your website, I will appreciate that :)


Anonymous said...

I found this script while lookng for a random password generator. While it is good, I am going to modify the function slightly to make it more customizable for people without modifying the variables inside the function.
Thanks for the code!

Ayomide Fagbohungbe said...

nice work, can you create a form for $1 variable

rize wordpress said...

Our expertise lies in PHP/Smarty, Core PHP, ROR, MVC framework, Flex, Yahoo Cart, Open Source CMS integration and customization
PHP Programmers

mary Brown said...

I have read your blog its very attractive and impressive. I like it your blog.

PHP Training in Chennai | Online PHP Training

PHP Training in Chennai | Online PHP Training