PHP generates a random password, easy and fast, can randomly generate a secure and reliable password, I hope this article will be helpful to everyone.
PHP generates a secure random password program instance code as follows:
<?php
Header ("Content-type:text/html;charset=utf-8");
function Getrandpass ($length = 6) {
$password = ";
Add the characters you want to the following string, which is the number 0-9 and the 26 English letters by default
$chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$char _len = strlen ($chars);
for ($i =0; $i < $length; $i + +) {
$loop = Mt_rand (0, ($char _len-1));
Take this string as an array, randomly take out a character, and loop it into the number of bits you need.
$password. = $chars [$loop];
}
return $password;
}
Echo Getrandpass (12); Randomly generate a 12-digit password
Open Source Code phpfensi.com
?>
Example 2, with the first one a little like
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
3, repeat the second step n times, can get the length of the password of n
The instance code is as follows:
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 the $chars;
The second type is to take any element $chars the character array
$password. = substr ($chars, Mt_rand (0, strlen ($chars)-1), 1);
$password. = $chars [Mt_rand (0, strlen ($chars)-1)];
}
return $password;
}