This article mainly describes how to generate a random password in php. The principle is to preset some character strings to be generated, including digits such as Pinyin and some special characters, in this way, we randomly take characters to make up our thoughts.
This article mainly describes how to generate a random password in php. The principle is to preset some character strings to be generated, including digits such as Pinyin and some special characters, in this way, we randomly take characters to make up our thoughts.
Code 1:
Generate a random password function. The generated password is a random string of lowercase letters and numbers. The length can be customized. Relatively speaking, this is relatively simple
The Code is as follows:
/*
* Php automatically generates a new password User-Defined Function (with instance 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 )."
";
?>
The following summarizes some examples for your reference.
Example 1
Simplest Generation Method
The Code is 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 33-126, for example, 35,
2. convert 35 to corresponding ASCII characters, for example, 35 #
3. Repeat steps 1 and 2 n and connect them to a n-bit password.
The Code is 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 to pass the length parameter $ pw_length = 6
Echo create_password (6 );
Instance
The Code is as follows:
Mt_srand (double) microtime () * 1000000 );
Function gen_random_password ($ password_length = 32, $ 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;
}
?>
Php Password generator v 4.0
Password generator v4.0 by freemouse
If (isset ($ _ GET ['password _ length']) {
If (preg_match ("/([0-9] {1, 8})/", $ _ GET ['password _ length']) {
Print ("password generated successfully:
". Gen_random_password ($ _ GET ['password _ length'])."
N ");
} Else {
Print ("Incorrect password length!
N ");
}
}
Print <end
Specify the length of the generated password for the password:
End;
?>
Example 4
1. preset a string $ chars, including A-z, a-Z, 0-9, and some special characters
2. Random character in $ chars string
3. Repeat Step 2 n times to obtain a password with a length of n.
The Code is as follows:
Function generate_password ($ length = 8 ){
// Password character set, which can be any character you need
$ Chars = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789! @ # $ % ^ & * ()-_ [] {}<> ~ '+ = ,.;:/? | ';
$ Password = '';
For ($ I = 0; $ I <$ length; $ I ++)
{
// Two character acquisition methods are provided here
// Use substr to intercept any character in $ chars;
// The second method is to take any element of the character array $ chars.
// $ Password. = substr ($ chars, mt_rand (0, strlen ($ chars)-1), 1 );
$ Password. = $ chars [mt_rand (0, strlen ($ chars)-1)];
}
Return $ password;
}
The tested performance is inferior to the following:
1. preset a character array $ chars, including A-z, a-Z, 0-9, and some special characters
2. Use array_rand () to randomly select $ length elements from the array $ chars.
3. Extract the string from the array $ chars Based on the obtained key name array $ keys. The disadvantage of this method is that the same characters are not repeated.
The Code is as follows: