Last updated on December 27th, 2022 at 10:24 am

A developer needs this script handy for configuring a website that can generate random password for the user in order to login to a website or may be during a user registration process. I am explaining two different methods of generating random password.

You can easily increase or decrease the length of the password accordingly. There are two sections in this tutorial.

Random Password

Here we are going to create a function that generates random password. Modify the number passed to the function to change the length of the password.

function Random_Password($length) {
srand(date("s"));
$possible_charactors = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$string = "";
while(strlen($string)< $length) {
$string .= substr($possible_charactors, rand()%(strlen($possible_charactors)),1);
}
return($string);
}
echo Random_Password(8); //Make password of 8 chars

Save the above code on a PHP file and run the page.

Check out this Demo

Pronounceable Password

In this script we create a pronounceable password, that means you can kind of read it unlike the random one above.

function genpassword($length){
srand((double)microtime()*1000000);
$vowels = array("a", "e", "i", "o", "u");
$cons = array("b", "c", "d", "g", "h", "j", "k", "l", "m", "n", "p", "r", "s", "t", "u", "v", "w", "tr",
"cr", "br", "fr", "th", "dr", "ch", "ph", "wr", "st", "sp", "sw", "pr", "sl", "cl");
$num_vowels = count($vowels);
$num_cons = count($cons);
for($i = 0; $i < $length; $i++){
$password .= $cons[rand(0, $num_cons - 1)] . $vowels[rand(0, $num_vowels - 1)];
}
return substr($password, 0, $length);
}
echo genpassword(10);

To modify the length of password, all you have to do is change the number from 10 to anything of your choice.

Demo

Leave a Reply

Your email address will not be published. Required fields are marked *