PHP Random Password

written by: Stratus Huo Quan; article published: year 2007, month 12;


In: Root » Computers and technology » PHP » PHP Random Password

Dutch French Spanish Portuguese Italian German Japanese Chinese Korean Russian Arabic Bookmark and Share this Article

In some applications, it can be useful to generate a random password, such as setting the user's initial password and then letting the user change it if he or she wishes. The function shown in the following code generates a password with a number of random characters, and you can set the length of the password when you call the function:

<?php  
function randomPassword($length) {  
$possibleCharacters =  "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ";  
$characterLength = strlen($possibleCharacters);  
$seed = (double) microtime() * 1000000;  
srand($seed);  
$password = "";  
for($i=1;$i<=$length;$i++){  
$character = rand(1,$characterLength);  
$character = substr($possibleCharacters,$character, 1);  
$password .= $character;  
}  
return $password;  
}  
?>  

To call the function, you can use the following code:

<?php  
$randomPassword = randomPassword(8);  
echo $randomPassword;  
?>  

This would create a random eight-character password—for example, VcF1TG65 or BJuNj7ao.

To create the password, first you set up a string containing characters that can be used for the password. You then find the number of characters in the string with the PHP strlen function.

Next, you seed the PHP random number generator and use it to create a random number between 1 and the number of characters that can be used. The seed is first created by using the PHP microtime function to return the number of seconds since the Linux epoch date, and you then multiply this by 1 million so that you have a whole number, which will be different each time the code is run. Next, you use this number as a seed for the random number generator using the PHP srand command, so that it generates truly random numbers. Note that with PHP version 4.2.0 and upward, you don't have to seed the random number generator as it's done automatically.

This random number is then used to extract a single character from the string of allowed characters by reading the character at the position in the string equal to the random number. This character is then added to the new password, until the number of characters in the password matches the number specified when the function was called.

Disclaimer

1) E-articles is not responsible for the information contained by this article as well for any and all copyright infringements by authors and writers. E-articles is a free information resource. If you suspect this article for any copyright infringement, please read the terms of service and contact us to investigate the problem.
2) E-articles is not responsible for inaccuracies, falsehoods, or any other types of misinformation this article may contain and will not be liable for any loss or damage suffered by a user through the user's reliance on the information gained here.

link to this article