Home >Backend Development >PHP Tutorial >Summary of three methods for generating random passwords in PHP_PHP Tutorial

Summary of three methods for generating random passwords in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:34:17817browse

Using PHP to develop applications, especially website programs, often requires generating a random password. For example, a random password is generated when a user registers and a random password is generated when the user resets the password. . A random password is a string of fixed length. Here I have collected several methods of generating random strings for your reference.

Method 1:

1. Generate a random integer from 33 – 126, such as 35,

2. Convert 35 into the corresponding ASCII code character, such as 35 corresponding to #

3. Repeat the above steps 1 and 2 n times to connect into an n-digit password

This algorithm mainly uses two functions. The mt_rand (int $min, int $max) function is used to generate random integers, where $min – $max is the range of ASCII codes. Here it is 33-126, which can be used as needed. Adjustment range, for example, 97 – 122 bits in the ASCII code table correspond to the English letters a – z. For details, please refer to the ASCII code table; chr (int $ascii) function is used to convert the corresponding integer $ascii into the corresponding character.

Copy code The code is as follows:

function create_password($pw_length = 8)
{
$randpwd = '';
for ($i = 0; $i < $pw_length; $i++)
{
$randpwd .= chr(mt_rand(33, 126));
}
return $randpwd;
}
// Call this function and pass the length parameter $pw_length = 6
echo create_password(6);

Method 2:
1 , Preset a string $chars, including a – z, A – Z, 0 – 9, and some special characters
2. Randomly pick a character
in the $chars string 3. Repeat the second step Step n times, you can get a password of length n
Copy the code The code is as follows:

function generate_password( $length = 8 ) {
// Password character set, you can add any characters you need
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_ []{}<>~`+= ,.;:/?|';
$password = '';
for ( $i = 0; $i < $length; $i++ )
{
// Here are two Three ways to get characters
// The first is to use substr to intercept any character in $chars;
// The second is to get any element of the character array $chars
// $password. = substr($chars, mt_rand(0, strlen($chars) - 1), 1);
$password .= $chars[ mt_rand(0, strlen($chars) - 1) ];
}
return $password;
}

Method 3:

1. Preset a character array $chars, including a – z, A – Z, 0 – 9, and some special characters

2. Randomly select $length elements from the array $chars through array_rand()

3. According to the obtained key name array $keys, take out the characters from the array $chars and concatenate the string. The disadvantage of this method is that the same characters will not be retrieved repeatedly.

Copy code The code is as follows:

function make_password( $length = 8 )
{
// Password Character set, you can add any characters you need
$chars = array('a', 'b', 'c', 'd', 'e', ​​'f', 'g', 'h',
'i', 'j', 'k', 'l','m', 'n', 'o', 'p', 'q', 'r', 's',
't', 'u', 'v', 'w', 'x', 'y','z', 'A', 'B', 'C', 'D',
'E' , 'F', 'G', 'H', 'I', 'J', 'K', 'L','M', 'N', 'O',
'P', 'Q ', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y','Z',
'0', '1', ' 2', '3', '4', '5', '6', '7', '8', '9', '!',
'@','#', '$', '%', '^', '&', '*', '(', ')', '-', '_',
'[', ']', '{', '}' , '<', '>', '~', '`', '+', '=', ',',
'.', ';', ':', '/', '?', '|');
// Randomly select $length array element key names in $chars
$keys = ($chars, $length);
$password = '';
for($i = 0; $i < $length; $i++)
{
// Concatenate $length array elements into a string
$password .= $chars[$ keys[$i]];
}
return $password;
}

Time efficiency comparison
We use the following PHP code to calculate The above three random password generation functions generate a 6-digit password running time, and then make a simple comparison of their time efficiency.
Copy code The code is as follows:

function getmicrotime()
{
list($usec, $sec) = explode(" ",microtime());
return ((float)$usec + (float)$sec);
}
// Recording start time
$time_start = getmicrotime();
//Put the PHP code to be executed here, such as:
// echo create_password(6);
// Record end time
$time_end = getmicrotime ();
$time = $time_end - $time_start;
// Output the total running time
echo "Execution time $time seconds";
?>

The final result is:
Method one: 9.8943710327148E-5 seconds
Method two: 9.6797943115234E-5 seconds
Method three: 0.00017499923706055 seconds
OK It can be seen that the execution time of method one and method two is almost the same, while the running time of method three is slightly longer.
Original text: http://www.ludou.org/how-to-create-a-password-generator-using-php.html

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322424.htmlTechArticleUsing PHP to develop applications, especially website programs, often requires generating random passwords, such as generating random passwords for user registration. Users resetting their passwords also need to generate a random password. ...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn