4 ways to generate random passwords in PHP and performance comparisons

Source: Internet
Author: User
Tags add array execution functions php and php code string strlen

The use of PHP to develop applications, especially web programs, often need to generate random passwords, such as user registration to generate random passwords, user reset password also need to generate a random password. Random password is a string of fixed length, here I collected several methods to generate random strings, for your reference.

Method One:

1. Generate a random integer, such as 35, in 33–126

2, convert 35 to the corresponding ASCII code characters, such as 35 corresponding #

3, repeat the above 1, 2 steps n times, connected to the N-bit password

The algorithm mainly uses two functions, the Mt_rand (int $min, int $max) function is used to generate random integers, where $min – $max for the range of ASCII code, which takes 33-126, can adjust the range according to the need, such as the ASCII code table 97– 122-bit corresponding a–z letter, the specific reference to the ASCII table; the Chr (int $ascii) function is used to convert the corresponding integer $ascii to the corresponding character.

View Sourceprint?

 
  
  
  1. function Create_password ($PW _length = 8)
  2. {
  3. $randpwd = ';
  4. for ($i = 0; $i < $PW _length; $i + +)
  5. {
  6. $randpwd. = Chr (Mt_rand (33, 126));
  7. }
  8. return $randpwd;
  9. }
  10. Call the function, passing the length parameter $pw_length = 6
  11. Echo Create_password (6);

Method Two:

1, preset a string $chars, including A–z,a–z,0–9, as well as some special characters

2. Randomly take a character in a $chars string

3, repeat the second step n times, can get a length of n password

View Sourceprint?

 
 
  1. function Generate_password ($length = 8) {
  2. Password character set, you can add any character you need
  3. $chars = ' abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&* ()-_ []{}<>~ ' +=,.;:/?]
  4. $password = ';
  5. for ($i = 0; $i < $length; $i + +)
  6. {
  7. Here are two ways to get a character
  8. The first is to use SUBSTR to intercept any one character in the $chars;
  9. The second is to take any element $chars the character array
  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. }

Method Three:

1, preset a character array $chars, including a–z,a–z,0–9, as well as some special characters

2. Randomly select $length elements from the array $chars by Array_rand ()

3, according to the acquired key array group $keys, from the array $chars out character stitching string. The disadvantage of this method is that the same characters are not repeated.

View Sourceprint?

 
 
  1. function Make_password ($length = 8)
  2. {
  3. Password character set, you can add any character you need
  4. $chars = Array (' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' H ',
  5. ' I ', ' j ', ' K ', ' l ', ' m ', ' n ', ' o ', ' P ', ' Q ', ' R ', ' s ',
  6. ' t ', ' u ', ' V ', ' w ', ' x ', ' y ', ' z ', ' A ', ' B ', ' C ', ' D ',
  7. ' E ', ' F ', ' G ', ' H ', ' I ', ' J ', ' K ', ' L ', ' M ', ' N ', ' O ',
  8. ' P ', ' Q ', ' R ', ' S ', ' T ', ' U ', ' V ', ' W ', ' X ', ' Y ', ' Z ',
  9. ' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', '! ',
  10. ' @ ', ' # ', ' $ ', '% ', ' ^ ', ' & ', ' * ', ' (', ') ', '-', ' _ ',
  11. ' [', '] ', ' {', '} ', ' < ', ' > ', ' ~ ', ' ', ' + ', ' = ', ', ', ', ', ', ', '
  12. '.', ';', ':', '/', '?', '');
  13. Randomly taking $length array element key name in a $chars
  14. $keys = Array_rand ($chars, $length);
  15. $password = ';
  16. for ($i = 0; $i < $length; $i + +)
  17. {
  18. To concatenate $length array elements into strings
  19. $password. = $chars [$keys [$i]];
  20. }
  21. return $password;
  22. }

Method Four:

This method is the blue ideal reproduced in this article, a netizen to provide a new method, the algorithm is simple, short code, just because of the return value of the MD5 () function, the generated password only includes letters and numbers, but also a good way. Algorithm idea:

1, Time () get the current Unix timestamp

2, the first step to obtain the timestamp to MD5 () encryption

3, the second step of the encryption results, intercept the N-bit is the desired password

View Sourceprint?

 
  
  
  1. function Get_password ($length = 8)
  2. {
  3. $STR = substr (MD5 (Time ()), 0, 6);
  4. return $str;
  5. }

Time Efficiency comparison

We use the following PHP code to calculate the running time of the 4 random password generation functions above to generate 6-bit passwords, and then make a simple comparison of their time efficiency.

View Sourceprint?

 
 
    1. <?PHP&NBSP;
    2. function getmicrotime ()  
    3.     list ($usec,  $sec)  = explode (" ", Microtime ())  
    4.     return  (float) $usec  +  (float) $ SEC);  
    5. &NBSP;
    6. //  record start time  
    7. $time _ Start = getmicrotime ();  
    8.  
    9. //  place the PHP code to execute here, such as:  
    10. // echo create_password (6);  
    11. &NBSP;
    12. //  record end time  
    13. $time _end = getmicrotime ();  
    14. $time  =  $time _end -  $time _start; 
    15. &NBSP;
    16.  //  Output total running time   
    17. echo  "Execution time   $time  seconds";  
    18. ? > 

The final result is:

Method One: 9.8943710327148E-5 seconds

Method Two: 9.6797943115234E-5 seconds

Method Three: 0.00017499923706055 seconds

Method Four: 3.4093856811523E-5 seconds

We can see that method one and method two of the execution time is the same, method four running time is shortest, and method three of the running time is slightly longer.



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.