UsePHPDeveloping applications, especially website programs, often requiresGenerate random passwordIf a user registers to generate a random password, a random password is also required for the user to reset the password. The random password is a string of fixed length. Here I have collected several methods to generate random strings for your reference.
Method 1:
1. Generate a random integer in 33-126, for example, 35,
2. convert 35 to corresponding ASCII characters, for example, 35 #
3. Repeat steps 1 and 2 n and connect them to a n-bit 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 ASCII code range, here, the range is 33-126. You can adjust the range as needed. For example, 97-122 characters in the ASCII code table correspond to English letters a-z. For details, refer to the ASCII code table. chr (int $ ascii) the function is used to convert the corresponding integer $ ascii to the corresponding character.
Copy codeThe 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 to 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. Random character in $ chars string
3. Repeat Step 2 n times to obtain a password with a length of n.Copy codeThe Code is as follows: function generate_password ($ length = 8 ){
// Password character set, which can be any character you need
$ Chars = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789! @ # $ % ^ & * ()-_ [] {}<> ~ '+ = ,.;:/? | ';
$ Password = '';
For ($ I = 0; $ I <$ length; $ I ++)
{
// Two character acquisition methods are provided here
// Use substr to intercept any character in $ chars;
// The second method is to take 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. Use array_rand () to randomly select $ length elements from the array $ chars.
3. Extract the string from the array $ chars Based on the obtained key name array $ keys. The disadvantage of this method is that the same characters are not repeated.
Copy codeThe Code is as follows: function make_password ($ length = 8)
{
// Password character set, which can be any character 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 ', '! ',
'@', '#', '$', '%', '^ ','&','*','(',')','-', '_',
'[', ']', '{', '}', '<', '> ','~ ', ''',' + ',' = ',',',
'.',';',':','/','? ',' | ');
// Random $ length array element key names in $ chars
$ Keys = ($ chars, $ length );
$ Password = '';
For ($ I = 0; $ I <$ length; $ I ++)
{
// Concatenate $ length array elements into strings
$ Password. = $ chars [$ keys [$ I];
}
Return $ password;
}
Time Efficiency Comparison
We use the following PHP code to calculate the running time of the above three random password generation functions to generate six passwords, and then compare their time efficiency.Copy codeThe Code is as follows: <? Php
Function getmicrotime ()
{
List ($ usec, $ sec) = explode ("", microtime ());
Return (float) $ usec + (float) $ sec );
}
// Record start time
$ Time_start = getmicrotime ();
// Put the PHP code to be executed here, for example:
// 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 1: 9.8943710327148E-5 seconds
Method 2: 9.6797943115234E-5 seconds
Method 3: 0.00017499923706055 seconds
We can see that method 1 and method 2 have almost the same execution time, while method 3 has a longer running time.
Original article: http://www.ludou.org/how-to-create-a-password-generator-using-php.html