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
Copy codeThe Code is as follows:
<? Php
/*
* 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 (). "<br> ";
Echo "New Password:". genPassword (5, 10). "<br> ";
?>
The following summarizes some examples for your reference.
Example 1
Simplest Generation Method
Copy codeThe 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.
Copy codeThe 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
Copy codeThe Code is as follows:
<? Php
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;
}
?> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
<Html>
<Head>
<Title> php Password generator v 4.0 </title>
<Style type = "text/css">
Body {
Font-family: Arial;
Font-size: 10pt;
}
</Style>
</Head>
<Body>
<Span style = "font-weight: bold; font-size: 15pt;"> Password generator v4.0 by freemouse </span> <br/>
<? Php
If (isset ($ _ GET ['password _ length']) {
If (preg_match ("/([0-9] {1, 8})/", $ _ GET ['password _ length']) {
Print ("password generated successfully: <br/>
<Span style = "font-weight: bold"> ". gen_random_password ($ _ GET ['password _ length']). "</span> <br/> n ");
} Else {
Print ("Incorrect password length! <Br/> n ");
}
}
Print <end
Specify the length of the generated password for the password: <br/>
<Form action = "{$ _ SERVER ['php _ SELF ']}" method = "get">
<Input type = "text" name = "password_length">
<Input type = "submit" value = "generate">
</Form>
End;
?>
</Body>
</Html>
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.
Copy codeThe 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.
Copy codeThe Code is as follows:
Function make_password ($ length = 8)
{
// Password character set, which can be 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 ', '! ',
'@', '#', '$', '%', '^ ','&','*','(',')','-', '_',
'[', ']', '{', '}', '<', '> ','~ ', ''',' + ',' = ',',',
'.',';',':','/','? ',' | ');
// Random $ length array element key names in $ chars
$ Keys = array_rand ($ chars, $ length );
$ Password = '';
For ($ I = 0; $ I <$ length; $ I ++)
{
// Concatenate $ length array elements into strings
$ Password. = $ chars [$ keys [$ I];
}
Return $ password;
}