Use PHP to develop applications, especially web applications, often need to generate a random password, such as user registration to generate a random password, the user also need to reset the password to generate a random password. Random password is a string of fixed-length string, where I collected several ways to generate a random string for your reference.
method one:
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 steps 1, 2 n times, connected into a n-bit password
The algorithm mainly uses two functions, mt_rand (int $ min, int $ max) function is used to generate a random integer, where $ min - $ max is the range of ASCII code, here take 33 -126, you can adjust the range as needed, Such as ASCII code table 97 - 122 corresponding a - z English letters, specific reference ASCII table; chr (int $ ascii) function is used to convert the corresponding integer $ ascii corresponding characters.
function create_password ($ pw_length = 8)
{
$ randpwd = '';
for ($ i = 0; $ i <$ pw_length; $ i ++)
{
$ randpwd. = chr (mt_rand (33, 126));
}
return $ randpwd;
}
// Call this function, passing the length parameter $ pw_length = 6
echo create_password (6);
Method Two:
1, Preset a string $ chars, including a - z, A - Z, 0 - 9, and some special characters
2, in the $ chars string randomly select a character
3, repeat the second step n times, available length n password
function generate_password ($ length = 8) {
// password character set, you can add any character you want
$ chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789! @ # $% ^ & * () -_ [] {} <> ~ `+ = ,.;: /? |';
$ password = '';
for ($ i = 0; $ i <$ length; $ i ++)
{
// Here are two ways to get characters
// The first is to use substr to intercept any chars in chars;
// The second is to take any element of the chars array $ chars
// $ password. = substr ($ chars, mt_rand (0, strlen ($ chars) - 1), 1);
$ password. = $ chars [mt_rand (0, strlen ($ chars) - 1)];
}
return $ password;
}
Method three:
1, Preset an array of characters $ chars, including a - z, A - Z, 0 - 9, and some special characters
2, through the array_rand () from the $ chars array randomly selected $ length elements
3, according to the key array has been obtained $ keys, $ chars removed from the array 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 (aa, 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,
'@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_',
'[', ']', '{', '}', '<', '>', '~', '`,' + ',' = ',', '
','; ',': ',' / ','? ',' | ');
// randomly get $ length array element keys in $ chars
$ keys = ($ chars, $ length);
$ password = '';
for ($ i = 0; $ i <$ length; $ i ++)
{
// Connect $ length array elements into a string
$ password. = $ chars [$ keys [$ i]];
}
return $ password;
}
Time efficiency comparison
We use the following PHP code to calculate the above three random password generation function to generate 6-digit password of the running time, and thus their time efficiency for a simple comparison.
<? php
function getmicrotime ()
{
list ($ usec, $ sec) = explode ("", microtime ());
return ((float) $ usec + (float) $ sec);
}
// Record start time
$ time_start = getmicrotime ();
// put here to execute the PHP code, such as:
// echo create_password (6);
// Record the end time
$ time_end = getmicrotime ();
$ time = $ time_end - $ time_start;
// Output total running time
echo "execution time $ time seconds";
?>
The final result is:
Method one: 9.8943710327148E-5 seconds
Method two: 9.6797943115234E-5 seconds
Method three: 0.00017499923706055 seconds
It can be seen that the execution times of method one and method two are similar, and the execution time of method three is slightly longer.