There are many ways to generate random passwords, the simplest is to use the PHP Mt_rand () function to generate a series of numbers directly, let me introduce you to PHP generated random password program
The simplest way to mt_rand a function
Mt_rand () returns a random integer using the Mersenne Twister algorithm.
Example
In this case, we will return some random numbers:
| The code is as follows |
Copy Code |
|
Echo (Mt_rand ());
Echo (Mt_rand ());
Echo (Mt_rand (10,100));
?>
The output is similar to:
3150906288
513289678
35 |
The above relative safety index is very low, because all is the number Oh, the following another name
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
|
copy 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 $chars;
//The second is the number of characters to take Any element of the group $chars
//$password. = substr ($chars, Mt_rand (0, strlen ($chars)-1), 1);
$password. = $chars [Mt_rand (0, strlen ($chars)-1)];
}
return $password;
} |
A friend provides the return value of the MD5 () function, and the generated password only includes letters and numbers, but it's also a good idea. Algorithm idea:
1. Time () Gets the current Unix timestamp
2, the first step to obtain the timestamp for MD5 () encryption
3, the second step to encrypt the result, intercept n-bit is the desired password
| The code is as follows |
Copy Code |
|
function Get_password ($length = 8)
{
$STR = substr (MD5 (Time ()), 0, 6);
return $str;
} |
http://www.bkjia.com/PHPjc/631273.html www.bkjia.com true http://www.bkjia.com/PHPjc/631273.html techarticle There are many ways to generate random passwords, the simplest is to use the PHP Mt_rand () function to directly generate a series of numbers, I would like to introduce you to PHP generation random password program The simplest to do ...