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 letter, the specific reference to the ASCII table; the Chr (int $ascii) function is used to convert the corresponding integer $ascii to the corresponding character.
| The code is as follows |
Copy Code |
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, 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, 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
| The code is as follows |
Copy Code |
| function Generate_password ($length = 8) { //password character set, you can add any character you need $chars = ' Abcdefghij klmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&* ()-_ []{}<>~ ' +=,.;:/?| '; $password = '; for ($i = 0; $i < $length; $i + +) { //Here are two character capture methods //The first is to use SUBSTR to intercept any one character in the $chars; //The second is any element that takes a character array $chars $password. = substr ($chars, Mt_rand (0, strlen ($chars)-1), 1); $password. = $chars [Mt_rand (0, strlen ($chars)-1)]; } return $password; } |
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 letter, the specific reference to the ASCII table; the Chr (int $ascii) function is used to convert the corresponding integer $ascii to the corresponding character.
| The code is as follows |
Copy Code |
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, 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, 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
| The code is as follows |
Copy Code |
| function Generate_password ($length = 8) { //password character set, you can add any character you need $chars = ' Abcdefghij klmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&* ()-_ []{}<>~ ' +=,.;:/?| '; $password = '; for ($i = 0; $i < $length; $i + +) { //Here are two character capture methods //The first is to use SUBSTR to intercept any one character in the $chars; //The second is any element that takes a character array $chars $password. = substr ($chars, Mt_rand (0, strlen ($chars)-1), 1); $password. = $chars [Mt_rand (0, strlen ($chars)-1)]; } return $password; } |