Website Scripts And Cloud Tutorials

Generate random password using php


Feb 2, 2014

Last updated on May 11th, 2016 at 01:30 pm

Click to rate this tutorial!
[Total: 3 Average: 4.3]


Generate random password using php

A developer needs this script handy for configuring a website which generates random password for the user in order to login to a website. I am explaining two different methods of generating random password. You can easily increase or decrease the length of the password accordingly. Here I am generating a password length of 8 for one script and 10 for the other.

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

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

Another example a good Pronounceable Password Generator can be done like this

function genpassword($length){


$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); //makes a password of 10 chars
Click to rate this tutorial!
[Total: 3 Average: 4.3]

Leave a Reply

Your email address will not be published.