There are many methods to generate a random password. The simplest is to use the phpmt_rand () function to directly generate a string of numbers, next I will introduce you to the simplest way to generate a random password program in php. m
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.
In this example, we will return some random numbers. the code is as follows:
-
- 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 instance 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;
- }
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 instance code is as follows:
- Function get_password ($ length = 8)
- {
- $ Str = substr (md5 (time (), 0, 6 );
- Return $ str;
- }