Sometimes we need to use randomly generated username and password in the application, which can greatly improve the security of the application, generate random username and password in PHP can use the Mt_rand function or the RAND function, the RAND function has more application in the verification code, and the random code of the growth character usually needs the Mt_rand function.
Using PHP to generate random numbers can be applied in many places, such as designing random passwords for programs, simulating dice game applications, stone-and-scissors-game applications, and more.
The following is a two-function method for generating random numbers in PHP:
Copy Code code as follows:
Automatically generate user names randomly for users (length 6-13)
function Create_password ($PW _length = 4) {
$randpwd = ';
for ($i = 0; $i < $PW _length; $i + +) {
$randpwd. = Chr (Mt_rand (33, 126));
}
return $randpwd;
}
function Generate_username ($length = 6) {
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 is any element that takes the $chars of a character array
$password. = substr ($chars, Mt_rand (0, strlen ($chars)-1), 1);
$password. = $chars [Mt_rand (0, strlen ($chars)-1)];
}
return $password;
}
Call this function
$userId = ' user '. Generate_username (6);
$pwd = Create_password (9);