The mt_rand (int $ min, int $ max) function is used to generate random integers. $ min & amp; ndash; $ max indicates the ascii code range. here, 33-126 is used, you can adjust the range as needed, as shown in figure
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:
- 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. 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:
- 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 and the 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;
- }
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:
- 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 ";