In: Categories » Computers and technology » PHP » 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
1) Our website 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 infringements, please read the Terms of service and contact us to investigate the problem.
2) The E-articles directory team is not responsible for inaccuracies, falsehoods, or any other types of misinformation this tutorial 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. Please read the Terms of service
Useful tools and features
related articles
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 PHP code is incorrect. For example, the following code: <?php for($i=1;$i<10;$i++){ $output = "Current Iteration: " . $i . "<br>" echo $output; } ?> will return an error similar to the following: Parse error: parse error, unexpected T_ECHO in c:\webserver\test2.php on lin...
2. 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 HTML. How Do I Send an E-mail As HTML? As the PHP mail function defaults to sending plain text e-mails unless otherwise specified, a frequent question is how to send HTML e-mails using the mail function. The format for the mail function is as follows: mail($to, $subject, $message, $headers); ...
3. 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 the server, you need to use the PHP date function. You pass the date function a string, with special tokens in place of the date parameters you require. When the code is run, the tokens will be replaced with the date or time section that they represent. For example: <?php echo date("d/m/Y"); ?> outputs 11/02/2003 &...
4. 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 it from the source. Downloading PHP PHP is available as a free download from the PHP web site, http://www.php.net/ downloads.php. The file that you need to download is at the top of the page, in the section labeled Complete Source Code. The current file at the time of this writing was for PHP 4.3....
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 first step is to download PHP, which is available from the PHP downloads page at http://www.php.net/downloads.php. Scroll down the downloads page until you find the section labeled Windows Binaries. The current version at the time of this writing is PHP 4.3.0, and the...










