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!

Unknown said...

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

Unknown said...

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

Unknown said...

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

Ancy merina said...
This comment has been removed by the author.
Unknown 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



Austin said...

Nice post I have been searching for a useful post like this on salesforce course details, it is highly helpful for me and I have a great experience with this, Salesforce Training Dallas  

hpu ug 2nd year result 2022 said...

The information you have produced is so good and helpful, I will visit your website regularly.