Several Ways to Generate a Random Password Using PHP 

Using PHP to develop applications, especially web application is often necessary to generate a random password to generate a random password, such as user registration, user reset the password to generate a random password. Random password is a string of fixed-length string, I collected several methods to generate a random string for your reference.

Way 1:

1. In 33 - 126 to generate a random integer, such as 35.

2. 35 will be converted into the corresponding ASCII characters, such as 35 corresponding to #.

3. Repeat the above two steps n times, connected n-bit password.

The algorithm is mainly used in two functions, mt_rand (int min, int $ max) function is used to generate a random integer, of which $ min - $ max is the ASCII code range, take 33-126, according to the need to adjust the range such as the ASCII code table 97 - 122 corresponds to a - z letters of the alphabet, specifically refer to the ASCII code table; chr (int $ ascii) function is used to correspond to an integer $ ascii converted into the corresponding character.

function create_password ($pw_length=8) {
$Randpwd='';
for($i=0;$i<$pw_length; $i++)
{
$Randpwd.=Chr(mt_rand(33, 126));
}
return $randpwd;
}
//Call the function, pass the length parameter $pw_length=6
echo create_password(6);

Way 2:

1. Preset a string the chars, including a - z, A - Z 0 - 9, as well as some special characters.

2. Randomly taken from a character in the chars string.

3. Repeat step n can be the length of the password of n.

The function generate_password($length=8) {
//Password character set, you can add any characters you need
$Chars='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKL
MNOPQRSTUVWXYZ0123456789! @ # $% ^ & * ()-_ [] {} <> ~ `+ =,.; :/? | ';

$Password ='';
for ($ i = 0; $ i <$ length; $ i + +) {
/ / Here offers two characters for the way
/ / The first is using substr interception $ the chars in any one character;
/ / The second is to take the character array $ arbitrary elements of the chars
/ / $ Password = substr ($ the chars, mt_rand (0, strlen ($ the chars) - 1), 1);
$ Password = $ the chars [mt_rand (0, strlen ($ the chars) - 1)];
}
return $ password;
}

Way 3:

1. Preset an array of characters the chars, including a - z, A - Z, 0 - 9, as well as some special characters.

2. By array_rand () the chars from array $ selected randomly from $ length elements.

3. According to the obtained key name array $ keys, array $ the chars and remove the character string concatenation. The disadvantage of this method is the same character will not be repeated to take.

The function make_password ($ length = 8) {
/ / Password character set, you can add any characters you need
$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', '!', '@','#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '[', ']', '{', '}', '

//Random to take the $chars $length array element key
$keys = ($chars, $length);
$password = '';
for($i = 0; $i < $length; $i++) { //$length array element is connected to a string
$Password.=$chars[$keys[$i]];
}
return $password;
}

Time efficiency comparison

We use the following PHP code to calculate the above three random password generator function to generate 6 run-time password, and then 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 put to perform PHP code, such as:
//Echo create_password (6);

//Record the end time
$Time_end=getmicrotime();
$Time=$time_end - $time_start;

//Output to run the total time
echo "execution time $ time seconds";
?>
Ultimately the result is:

Method 1: 9.8943710327148E-5 seconds
Method 2: 9.6797943115234E-5 seconds
Methods: 0.00017499923706055 seconds

It can be seen that the execution time of method one and method two are similar, the methods of running time a little bit long.

 



This address is http://www.computerites.com/internet-skills/2012/03/several-ways-to-generate-a-random-password-using-php-449.html