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:
<?php
function createRandomPassword() {
    $chars = "abcdefghijkmnopqrstuvwxyz023456789";
    srand((double)microtime()*1000000);
    $i = 0;
    $pass = '' ;
    while ($i <= 9) {
        $num = rand() % 33;
        $tmp = substr($chars, $num, 1);
        $pass = $pass . $tmp;
        $i++;
    }
    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 :)

8 comments:

Anonymous said...

Hi!
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

kokila zenrays said...

Thanks for posting this article. Good useful information, Random PHP code generator. learn PHP Training in Bangalore

Ancy merina said...

this is extremely an exceptionally awesome blog. the data display in this blog will be extremely helpful for us. much thanks to you for imparting to us.
PHP Development Company In Bangalore | Best Services Website Design

Rohan kumar said...

Thank you for your post. This is excellent information. It is amazing and wonderful to visit your site
samsung Galaxy Note 9 smartphone
.Honor 8X smartphone
Huawei Mate 20 X smartphone
.OnePlus C type Earphones
..Apple iPhone XR specification
..Xiaomi Mi 8 Youth Edition smartphone
samsung Galaxy Note 9 Price
.Honor 8X Price
Huawei Mate 20 X Price
.OnePlus C type Price
..Apple iPhone XR Price
..Xiaomi Mi 8 Youth Editions Price
samsung Galaxy Note 9 Specification
.Honor 8X Specification
Huawei Mate 20 X Specification
.OnePlus C type Specification
..Apple iPhone XR Specification
..Xiaomi Mi 8 Youth Editions Specification
samsung Galaxy Note 9 Feature
.Honor 8X Feature
Huawei Mate 20 X Feature
.OnePlus C type Feature
..Apple iPhone XR Feature
..Xiaomi Mi 8 Youth Editions Feature



Anil kumar said...

Thank you for your post. This is excellent information. It is amazing and wonderful to visit your site
sarkari naukri
Latest Govt Jobs
सरकारी नौकरी
सरकारी रिजल्ट
sarkari update
sarkari result 2019
baba result 2019
Army vacancy
sarkari exam
freejobalert
Railway vacancy 2018