PHP code for generating random user names and passwords

Source: Internet
Author: User
Using PHP to generate random numbers can be applied in many places, such as designing random passwords for programs, simulating dice games, and stone scissors game applications.

Using PHP to generate random numbers can be applied in many places, such as designing random passwords for programs, simulating dice games, and stone scissors game applications.

Sometimes we need to use a random user name and password in the application, which can greatly improve the security of the application. To generate a random user name and password in PHP, you can use the mt_rand function or the rand function, the rand function applies more in the verification code, while the random code of the generated characters generally requires 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 games, and stone scissors game applications.

The following are two function methods for generating random numbers in PHP:

The Code is as follows:
// Automatically generate a random user name 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, which can be any character you need
$ Chars = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789! @ # $ % ^ & * ()-_ [] {}<> ~ '+ = ,.;:/? | ';
$ Password = '';
For ($ I = 0; $ I <$ length; $ I ++)
{
// Two character acquisition methods are provided here
// 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 );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.