When using PHP to develop applications, especially website programs, you often need to generate random passwords. This article collects several methods to generate random strings, which is helpful to you. If a user registers to generate a random password, the user must also generate a random password to reset the password. A random password is a string with a fixed length. This article provides several methods to generate random strings.
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 = {$ 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. a random character in the $ 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 = ($ chars, $ length) in $ chars; $ 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. 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.
By comparing the implementation process and time efficiency of the three methods, we learned three methods for generating random strings in PHP, hoping to help you in your future studies.