Code One:
Generate a random password function, the generated password is lowercase letters and numbers of random strings, the length can be customized. Relatively speaking, this is relatively simple
Copy the Code code as follows:
/*
* PHP automatically generate new password custom function (with example demo)
Applicable environment: Php5.2.x/mysql 5.0.x
* */
function Genpassword ($min = 5, $max = 8)
{
$validchars = "abcdefghijklmnopqrstuvwxyz123456789";
$max _char=strlen ($validchars)-1;
$length =mt_rand ($min, $max);
$password = "";
for ($i =0; $i < $length; $i)
{
$password. = $validchars [Mt_rand (0, $max _char)];
}
return $password;
}
echo "New password:". Genpassword (). "
";
echo "New password:". Genpassword (5,10). "
";
?>
Here is a summary of some examples of friends can refer to.
Example 1
The most concise method of generating
Copy the Code code as follows:
function Generatepassword ($length =8)
{
$chars = Array_merge (range (0,9),
Range (' A ', ' Z '),
Range (' A ', ' Z '),
Array ('! ', ' @ ', ' $ ', '% ', ' ^ ', ' & ', ' * '));
Shuffle ($chars);
$password = ";
for ($i =0; $i <8; $i + +) {
$password. = $chars [$i];
}
return $password;
}
Example 2
1, generate a random integer in the 33–126, such as 35,
2, convert 35 to the corresponding ASCII character, such as 35 corresponding #
3, repeat the above 1, 2 steps n times, connected to n-bit password
Copy the Code code as follows:
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, pass length parameter $pw_length = 6
Echo Create_password (6);
Instance
Copy CodeThe code is as follows:
Mt_srand (Double) microtime () * 1000000);
function Gen_random_password ($password _length = +, $generated _password = "") {
$valid _characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$chars _length = strlen ($valid _characters)-1;
for ($i = $password _length; $i--;) {
$generated _password. = $valid _characters[mt_rand (0, $chars _length)];
$generated _password. = substr ($valid _characters, (Mt_rand ()% (strlen ($valid _characters)), 1);
}
return $generated _password;
}
?>
<title>PHP Password Generator v 4.0</title>
Password Generator v4.0 by Freemouse
if (Isset ($_get[' password_length ')) {
if (Preg_match ("/([0-9]{1,8})/", $_get[' Password_length '])) {
Print ("Password generation succeeded:
" . Gen_random_password ($_get[' password_length '). "
n ");
} else {
Print ("Password length is incorrect!
n ");
}
}
Print <<< End
Generate the length of the password that you specify for the password:
End
?>
Example 4
1. Preset a string $chars, including a–z,a–z,0–9, and some special characters
2. Randomly take a character in a $chars string
3, repeat the second step n times, can get the length of the password of n
Copy the Code code as follows:
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 a character
The first is to use SUBSTR to intercept any one character in the $chars;
The second type is to take any element $chars the character array
$password. = substr ($chars, Mt_rand (0, strlen ($chars)-1), 1);
$password. = $chars [Mt_rand (0, strlen ($chars)-1)];
}
return $password;
}
The above test performance is inferior to the following
1, preset a character array $chars, including a–z,a–z,0–9, and some special characters
2. Randomly select $length elements from the array $chars by Array_rand ()
3, according to the obtained key an array group $keys, from the array $chars remove the character stitching string. The disadvantage of the method is that the same characters are not repeated.
Copy the Code code as follows:
function Make_password ($length = 8)
{
Password character set, you can add any character 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 ', '! ',
' @ ', ' # ', ' $ ', '% ', ' ^ ', ' & ', ' * ', ' (', ') ', '-', ' _ ',
' [', '] ', ' {', '} ', ' < ', ' > ', ' ~ ', ' ', ' + ', ' = ', ', ',
'.', ';', ':', '/', '?', '|');
Randomly take $length number of element key names in the $chars
$keys = Array_rand ($chars, $length);
$password = ";
for ($i = 0; $i < $length; $i + +)
{
Concatenate $length array elements into a string
$password. = $chars [$keys [$i]];
}
return $password;
}
http://www.bkjia.com/PHPjc/824893.html www.bkjia.com true http://www.bkjia.com/PHPjc/824893.html techarticle code One: Generate a random password function, the generated password is lowercase letters and numbers of random strings, the length can be customized. Relatively speaking, this relatively simple copy code code ...