The mt_rand (int $ min, int $ max) function is used to generate random integers, where $ minamp; ndash; $ max is the ascii code range. here, 33-126 is used, you can adjust the range as needed, such as... the mt_rand (int $ min, int $ max) function is used to generate a random integer. $ min-$ max indicates the ascii code range. here, 33-126 is used. you can adjust the range as needed, for example, The-characters in the ascii code table correspond to letters a-z. for details, refer to the ascii code table. The chr (int $ ascii) function is used to convert the corresponding integer $ ascii to the corresponding characters, the code is as follows:
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. the code is as follows:
~ '+ = ,.;:/? | '; $ 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 and the code is as follows:
','~ ', ''',' + ',' = ',',','.',';',':','/','? ',' | '); // 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 ;}?>
Comparison of time efficiency: We use the following php code to calculate the running time of the above three random password generation functions to generate a six-digit password, and then make a simple comparison of their time efficiency, the code is as follows:
Article link:
Save this article for favorites!