There are many ways to generate random passwords. The simplest is to use the php mt_rand() function to directly generate a string of numbers. Now I will introduce to you the php program to generate random passwords
The simplest method is the mt_rand function
mt_rand() returns a random integer using the Mersenne Twister algorithm.
Example
In this case, we return some random numbers:
The code is as follows
代码如下 |
复制代码 |
echo(mt_rand());
echo(mt_rand());
echo(mt_rand(10,100));
?>
输出类似:
3150906288
513289678
35
|
|
Copy code
|
echo(mt_rand());
echo(mt_rand());
代码如下 |
复制代码 |
function generate_password( $length = 8 ) {
// 密码字符集,可任意添加你需要的字符
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_ []{}<>~`+=,.;:/?|';
$password = '';
for ( $i = 0; $i < $length; $i++ )
{
// 这里提供两种字符获取方式
// 第一种是使用 substr 截取$chars中的任意一位字符;
// 第二种是取字符数组 $chars 的任意元素
// $password .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
$password .= $chars[ mt_rand(0, strlen($chars) - 1) ];
}
return $password;
}
|
echo(mt_rand(10,100));
?>
Output similar to:
3150906288
代码如下 |
复制代码 |
function get_password( $length = 8 )
{
$str = substr(md5(time()), 0, 6);
return $str;
}
|
513289678 |
35
The safety index above is relatively low because it is all numbers. Here is another one
1. Preset a string $chars, including a – z, A – Z, 0 – 9, and some special characters
2. Randomly pick a character from the $chars string
The code is as follows
|
Copy code
function generate_password( $length = 8 ) {
// Password character set, you can add any characters you need
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_ []{}<>~`+=,.;:/?|';
$password = '';
for ( $i = 0; $i < $length; $i++ )
{
// There are two ways to get characters here
//The second 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;
}
Because of the return value of the md5() function provided by a friend, the generated password only includes letters and numbers, but it is still a good method. Algorithmic thinking:
1. time() gets the current Unix timestamp
2. Encrypt the timestamp obtained in the first step with md5()
3. Use the encryption result of the second step and intercept n digits to get 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.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631273.htmlTechArticleThere are many ways to generate random passwords. The simplest is to use the php mt_rand() function to directly generate a string of numbers. Okay, let me introduce to you the simplest way to generate a random password using PHP...
|
|
|