PHP generates a summary of random passwords, and php generates a summary.
When using PHP to develop applications, especially website programs, you often need to generate random passwords. For example, if you register a user to generate a random password, you also need to generate a random password 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.
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.
Function generate_password ($ length = 8) {// you can add the character set $ chars = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789 as needed! @ # $ % ^ & * ()-_ [] {}<> ~ '+ = ,.;:/? | '; $ Password = ''; for ($ I = 0; $ I <$ length; $ I ++) {// two character acquisition methods are provided here. // The first method is to use substr to intercept any character in $ chars; // The second type 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.
Function make_password ($ length = 8) {// Password character set. You can add the required characters $ 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', '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 name $ keys = array_rand ($ chars, $ length) in $ chars; $ password = ''; for ($ I = 0; $ I <$ length; $ I ++) {// concatenate $ length array elements into strings $ password. = $ chars [$ keys [$ I];} return $ password ;}
Method 4:
This method is a new method provided by a netizen after being reproduced by the Blue ideal in this article. The algorithm is simple and the code is short, just because of the return value of the md5 () function, the generated password only contains letters and numbers, but it is also a good method. Algorithm idea:
1. time () gets the current Unix Timestamp
2. encrypt the timestamp obtained in step 1 with md5 ()
3. extract the data encrypted in step 2 to obtain the desired password.
function get_password( $length = 8 ) { $str = substr(md5(time()), 0, 6); return $str;}
Time Efficiency Comparison
We use the following PHP code to calculate the running time of the above four random password generation functions to generate six passwords, and then compare their time efficiency.
<? Phpfunction 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";?>
Method 5:
Function rand_string ($ len = 16, $ keyword = '') {if (strlen ($ keyword)> $ len) {// The keyword cannot be longer than the total length. return false ;} $ str = ''; $ chars = 'abcdefghijkmnpqrstuvwxyz23456789abcdefghijkmnpqrstuvwxyz '; // remove 1 from the letter l to prevent confusion if ($ len> strlen ($ chars )) {// repeated strings are too long for a certain number of times $ chars = str_repeat ($ chars, ceil ($ len/strlen ($ chars);} $ chars = str_shuffle ($ chars ); // disrupt the string $ str = substr ($ chars, 0, $ len); if (! Empty ($ keyword) {$ start = $ len-strlen ($ keyword); $ str = substr_replace ($ str, $ keyword, mt_rand (0, $ start ), strlen ($ keyword); // insert keywords from random locations} return $ str;} echo rand_string (16, "AB"); // output example: V8bNY6SmkeywordB?>
The final result is:
Method 1: 9.8943710327148E-5 seconds
Method 2: 9.6797943115234E-5 seconds
Method 3: 0.00017499923706055 seconds
Method 4: 3.4093856811523E-5 seconds
We can see that method 1 and method 2 have almost the same execution time, And Method 4 has the shortest time, while method 3 has a longer running time.