How to create a random password using php

Post Reply
User avatar
Saman
Lieutenant Colonel
Lieutenant Colonel
Posts: 828
Joined: Fri Jul 31, 2009 10:32 pm
Location: Mount Lavinia

How to create a random password using php

Post by Saman » Fri Jul 15, 2011 6:34 pm

Nice little function that will generate a completely random password.

Code: Select all

<?php 
 
/** 
 * The letter l (lowercase L) and the number 1 
 * have been removed, as they can be mistaken 
 * for each other. 
 */ 
 
function createRandomPassword() { 
 
    $chars = "abcdefghijkmnopqrstuvwxyz023456789"; 
    srand((double)microtime()*1000000); 
    $i = 0; 
    $pass = '' ; 
 
    while ($i <= 7) { 
        $num = rand() % 33; 
        $tmp = substr($chars, $num, 1); 
        $pass = $pass . $tmp; 
        $i++; 
    } 
 
    return $pass; 
 
} 
 
// Usage 
$password = createRandomPassword(); 
echo "Your random password is: $password"; 
 
?>
Shalom
Corporal
Corporal
Posts: 3
Joined: Mon Sep 26, 2011 9:09 am

Re: How to create a random password using php

Post by Shalom » Sun Dec 25, 2011 11:54 am

Superb work bro.... :biggrin: :biggrin:
User avatar
Saman
Lieutenant Colonel
Lieutenant Colonel
Posts: 828
Joined: Fri Jul 31, 2009 10:32 pm
Location: Mount Lavinia

Re: How to create a random password using php

Post by Saman » Fri Jan 06, 2012 6:15 pm

Another version of a php password generator Another powerful password generator using php.
Post Reply

Return to “PHP & MySQL”