There are many methods to generate a random password. The simplest is to use the php mt_rand () function to directly generate a string of numbers. Next I will introduce you to the php random password Generation Program.
The simplest method is the mt_rand function.
Mt_rand () returns a random integer using the Mersenne Twister algorithm.
Example
In this example, we will return some random numbers:
The Code is as follows: |
Copy code |
<? Php Echo (mt_rand ()); Echo (mt_rand ()); Echo (mt_rand (10,100 )); ?> The output is similar: 3150906288 513289678 35 |
The above is relatively low in the security index, because it is all numbers.
1. preset a string $ chars, including A-z, a-Z, 0-9, and some special characters
2. Random character in $ chars string
The Code is as follows: |
Copy code |
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; } |
Because of the md5 () function returned value provided by a friend, 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.
The Code is as follows: |
Copy code |
Function get_password ($ length = 8) { $ Str = substr (md5 (time (), 0, 6 ); Return $ str; } |