Home > Article > Backend Development > PHP randomly generates username and random password_PHP tutorial
//Automatically generate a random username for the user (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 characters you need
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_ []{}<>~`+=,.;:/?|';
$password = '';
for ( $i = 0; $i < $length; $i++ )
// Here two character acquisition methods
// The first is to 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;
}
// Call this function
$userId = 'user'.generate_username(6);
$pwd = create_password(9);
Excerpted from Hurry’s column