PHP Random Password

by Stratus Huo Quan.

Share
|
Homepage | Submit your article | Contact | TOS
More articles on php  

You are here: Categories » Computers and technology » PHP

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.

Leave a comment or ask a question
Total comments: 0

PHP Disclaimer

  • The e-articles directory is not responsible for any and all copyright infringements by writers and authors. If you suspect the information contained by this page for any copyright infringements, please contact us to investigate the issue
How PHP useful for business and individual - PHP has become a very reliable and infallible platform for building complex websites. One of the most important and most powerful constituent elements of PHP is its ability to interact with databas (more...)
Benefits of PHP programmers in freelance programming industry - Before you hire a contract programmer to put more features into your website, you need to know which programming language will be helpful for your website as well as affordable for your budget. Sup (more...)
Installing PHP with Apache on Windows - We try to install PHP into Apache so it can process PHP pages and static HTML pages. We assume that you have installed and tested Apache. Downloading PHP The firs (more...)
How to Install PHP into Apache - In this section, we look at how to install PHP into Apache. The first step is to download it from the PHP web site. There are other sources for PHP around the Web, but it is much easier to get (more...)
PHP Date and Time - In this tutorial, we look at commonly asked questions regarding the date function in PHP. How Do I Read the Date or Time from the Server? To read the date or time from t (more...)
PHP mail - In this article, we are going to look at some of the frequently asked questions regarding e-mail and PHP. We will begin by looking at a more fundamental issue: how to actually send an e-mail as (more...)
Common PHP Errors - In this tutorial, we are going to look at some of the common PHP errors that occur and how to solve them. Parse Errors A parse error occurs when the format of your (more...)

 
free content
    Copyright © 2006 - 2012 e-articles.info.
The texts, articles and tutorials in the directory are property of their respective owners and authors.