Php generates random password program code

Source: Internet
Author: User
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:

  1. Echo (mt_rand ());
  2. Echo (mt_rand ());
  3. Echo (mt_rand (10,100 ));
  4. ?>
  5. // The output is similar:
  6. // 3150906288
  7. // 513289678
  8. // 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:

  1. Function generate_password ($ length = 8 ){
  2. // Password character set, which can be any character you need
  3. $ Chars = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789! @ # $ % ^ & * ()-_ [] {}<> ~ '+ = ,.;:/? | ';
  4. $ Password = '';
  5. For ($ I = 0; $ I <$ length; $ I ++)
  6. {
  7. // Two character acquisition methods are provided here
  8. // Use substr to intercept any character in $ chars;
  9. // The second method is to take any element of the character array $ chars.
  10. // $ Password. = substr ($ chars, mt_rand (0, strlen ($ chars)-1), 1 );
  11. $ Password. = $ chars [mt_rand (0, strlen ($ chars)-1)];
  12. }
  13. Return $ password;
  14. }

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:

  1. Function get_password ($ length = 8)
  2. {
  3. $ Str = substr (md5 (time (), 0, 6 );
  4. Return $ str;
  5. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.