The Mt_rand (int $min, int $max) function is used to generate a random integer, where the $min– $max the range of ASCII code, where it takes 33-126 to adjust the range as needed, such as the 97–122 bit corresponding to a–z in the ASCII code table. Refer to the ASCII Code table specifically, the CHR (int $ascii) function is used to convert the corresponding integer $ascii to the corresponding character.
Code:
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);
Method Two:
1, preset a string $chars, including A–z, A–z, 0–9, and some special characters;
2. Randomly take a character in the $chars string;
3, repeat the second step n times, can get the length of the password of N.
Code:
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;
}
1 2
http://www.bkjia.com/PHPjc/631355.html www.bkjia.com true http://www.bkjia.com/PHPjc/631355.html techarticle The mt_rand (int $min, int $max) function is used to generate random integers, where $min$max is the ASCII code range, which takes 33-126, which can be adjusted as needed, such as 97,122-bit correspondence in the ASCII code table ...