Summary of methods for generating random passwords in PHP

Source: Internet
Author: User
Tags character set chr comparison execution functions integer php code 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 English letters, specific reference to the ASCII table; Chr (int $ascii) function is used to convert the corresponding integer $ascii to the corresponding characters, the specific function 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;
}

Use the following method, which generates a random password with a length of 6:

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

function Generate_password ($length =8) {
    //password character set, you can add any character you need
    $chars = ' abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&* ()-_ []{}<>~ ' +=,.;:/? ';
    $password = ';
    For ($i =0 $i < $length $i + +) {//
        Here are two ways
        to get the characters///The first is to use SUBSTR to intercept any one character in $chars;//The
        second is to take an array of characters $chars of any element
        //$password. =SUBSTR ($chars, Mt_rand (0,strlen ($chars)-1), 1);
        $password. = $chars [Mt_rand (0,strlen ($chars)-1)];
    }
    return $password;
}

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.

function Make_password ($length =8) {
    //password character set, you can add any character you want
    $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 ', '! ',
    ' @ ', ' # ', ' $ ', '% ', ' ^ ', ' & ', ' * ', ' (', ') ', '-', ' _ ',
    ' [', '] ', ' {', '} ', ' < ', ' > ', ', ', ' ', ' + ', ' = ', ', ', '
    . ', '; ', ': ', '/', '? ',
    ' The $length array element key name
    $keys =array_rand ($chars, $length) are randomly taken in the $chars;
    $password = ';
    For ($i =0 $i < $length; $i + +) {
        //Connect $length array elements to string
        $password. = $chars [$keys [$i]];
    return $password;
}

Method Four:

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

function Get_password ($length =8) {
    $str =substr (MD5 (Time ()), 0,6);
    return $str;
}

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.

<?php
function Getmicrotime () {
    list ($usec, $sec) =explode ("", Microtime ());
    Return ((float) $usec + (float) $sec);
}
Record start time
$time _start=getmicrotime ();
Here is the PHP code to execute, such as:
//Echo Create_password (6);
Record end time
$time _end=getmicrotime ();
$time = $time _end-$time _start;
 Output run total time 
echo "Execution time $time seconds";
? >

For the above test program to run the time method, can also refer to this site article:

PHP Calculation program run time class

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

In summary, we can see that the 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 a little 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.