Several methods for generating random passwords in php

Source: Internet
Author: User
The mt_rand (int $ min, int $ max) function is used to generate random integers, where $ minamp; ndash; $ max is the ascii code range. here, 33-126 is used, you can adjust the range as needed, such as... the mt_rand (int $ min, int $ max) function is used to generate a random integer. $ min-$ max indicates the ascii code range. here, 33-126 is used. you can adjust the range as needed, for example, The-characters in the ascii code table correspond to letters a-z. for details, refer to the ascii code table. The chr (int $ ascii) function is used to convert the corresponding integer $ ascii to the corresponding characters, the code is as follows:

 

Method 2:

1. preset a string $ chars, including a-z, a-z, 0-9, and some special characters;

2. a random character in the $ chars string;

3. Repeat step 2 n times to obtain a password with a length of n. the code is as follows:

 ~ '+ = ,.;:/? | '; $ Password = ''; for ($ I = 0; $ I <$ length; $ I ++) {// Two character acquisition methods are provided here. // The first method is to use substr to intercept any character in $ chars; // The second type 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 ;}?>

Method 3:

1. preset a character array $ chars, including a-z, a-z, 0-9, and some special characters;

2. use array_rand () to randomly select $ length elements from the array $ chars;

3. extract the string from the array $ chars based on the obtained key name array $ keys.

The disadvantage of this method is that the same characters are not repeated and the code is as follows:

 ','~ ', ''',' + ',' = ',',','.',';',':','/','? ',' | '); // Random $ length array element key name $ keys = ($ chars, $ length) in $ chars; $ password = ''; for ($ I = 0; $ I <$ length; $ I ++) {// concatenate $ length array elements into strings $ password. = $ chars [$ keys [$ I];} return $ password ;}?>

Comparison of time efficiency: We use the following php code to calculate the running time of the above three random password generation functions to generate a six-digit password, and then make a simple comparison of their time efficiency, the code is as follows:

 


Article link:

Save this article for favorites!

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.