Methods for generating random passwords in php _ PHP Tutorial

Source: Internet
Author: User
Php generates random passwords. When you want to generate a random password, you may first think of using the uniquid () function, but if we use array_rand () and range (), you can achieve this perfectly. when you want to generate a random password, you may first think of using the uniquid () function, but if we use array_rand () and range.


Method 1,

The code is as follows:

Header ("Content-type: text/html; charset = utf-8 ");

Function randCode ($ length ){
$ Ranges = array (range ('A', 'z'), range ('A', 'z'), range (1, 9 ));
$ Code = '';
For ($ I = 0; $ I <$ length; $ I ++ ){
$ Rkey = array_rand ($ ranges );
$ Vkey = array_rand ($ ranges [$ rkey]);
$ Code. = $ ranges [$ rkey] [$ vkey];
}
Return $ code;
}

Echo "www. bKjia. c0m ";
Echo randCode (5); // outputs such as 3IxY8, E6HOv, and 1qHiy
?>

Method 2,

1. Generate a random integer in 33-126, for example, 35,

2. convert 35 to corresponding ASCII characters, for example, 35 #

3. Repeat steps 1 and 2 n and connect them to a n-bit password.

This algorithm mainly uses two functions. the mt_rand (int $ min, int $ max) function is used to generate random integers, where $ min-$ max is the ASCII code range, here, the range is 33-126. you can adjust the range as needed. for example, 97-122 characters in the ASCII code table correspond to English letters a-z. for details, refer to the ASCII code table. chr (int $ ascii) the function is used to convert the corresponding integer $ ascii to the corresponding character.

The code is as follows:

Function create_password ($ pw_length = 8)

{

$ Randpwd = '';

For ($ I = 0; $ I <$ pw_length; $ I ++)

{

$ Randpwd. = chr (mt_rand (33,126 ));

}

Return $ randpwd;

}

// Call this function to pass the length parameter $ pw_length = 6

Echo create_password (6 );

Method 3,

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 );

Except uniquid () function, but if we use array_rand () and range (), we can implement this perfectly...

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.