PHP Random Password


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.

Legal Disclaimer

Our website is not responsible for the information contained by this article. Webworldarticles.com is a free articles resource thus practically any visitor can submit an article. However if you notice any copyrighted material, please contact us and we will remove the article(s) in discussion right away.


This article was sent to us by: Stratus Huo Quan at 12102007

Related Articles

1. 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 afford...

2. 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...

3. PHP Development: Turn Your Website into Multi Featured Web Application
Are you looking for developing a web application with charming features which have a range integrated database? PHP is one of the best technologies for web applications int...

4. How to choose a Programming Language for Web Application
Web Application is an application that is accessed over the Internet and hosted in a browser-controlled environment. Web application is developed using...

5. Advantages of Using PHP Frameworks in Contract PHP Programming
PHP is known as server side scripting language and used in all known operating systems like Windows, UNIX, Linux etc. Nowadays PHP frameworks are a step ahead of the genera...

6. Tips on how to hire a PHP developer
PHP is an open-source, general-purpose scripting language that is particularly suited for Web Development & can be written in HTML. PHP is a strong Server-Side language...

7. How Programming Languages Work
OUR most fundamental tool as intelligent beings is language. It is via language that you learn new information and share your knowledge, feelings, and experiences with ot...