Random password generator with php

In this post we are just creating a PHP function that provide us a random password string. This Password string is the combination of small and capital alphabets and digits, that help us to generate random password string. To generate the random password we are using here `mt_rand()` a PHP function. We can also use `rand()`, because both the functions provide the random strings but `mt_rand()` providing four time better random string then `rand()` function.

As you see below, we have created a function with the name `randomPasswordGenerator()`, this function return a random password string with the length 8 by default. And if you want to generate the password string length according to your choice then simply put the string length in the function call something like this : `randomPasswordGenerator(6)`. Hope you will enjoy this post, If you have something better example of suggestion then please come to the Comment section..

function randomPasswordGenerator($length = 8) {
    $string = 'bcdfghjklmnprstvwxzaeiou123654789ABCDEFGHIJKLMNPQRSTUVWXYZ';

    for ($i = 0; $i < $length; $i++) 
        $result .= ($i%2) ? $string[mt_rand(19, strlen($string))] : $string[mt_rand(0, 18)];

    return $result;
}

 

How to Use this function to generate random Password:

echo randomPasswordGenerator();

 

Posted by Jogesh Sharma

Jogesh Sharma is a web developer and blogger who loves all the things design and the technology, He love all the things having to do with PHP, WordPress, Joomla, Magento, Durpal, Codeigniter, jQuery, HTML5 etc. He is the author of this blog.