Jump to content

create random password


arathi

Recommended Posts

Hi,

 

I have a function that I always use for passwords:

 

function generatePassword ($length = {

// start with a blank password
$Password = "";
// define possible characters
$possible = "0123456789bcdfghjkmnpqrstvwxyz"; 

// set up a counter
$i = 0;

// add random characters to $password until $length is reached
while ($i < $length) { 
// pick a random character from the possible ones
$char = substr($possible, mt_rand(0, strlen($possible)-1), 1);

// we don't want this character if it's already in the password
if (!strstr($Password, $char)) { 
  $Password .= $char;
  $i++;
    
   }

   }

   // done!
   return $Password;
} 

 

You can then create a password by:

 

$password = generatePassword();

 

Hope this helps

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.