Several methods of random password generation _php tutorial

Source: Internet
Author: User
Random passwords are simple, but there are a lot of places to follow, all made up of special strings, let's look at the following three ways.

Method One:

1, generate a random integer in the 33–126, such as 35,

2, convert 35 to the corresponding ASCII character, such as 35 corresponding #

3, repeat the above 1, 2 steps n times, connected to n-bit password

The algorithm mainly uses two functions, Mt_rand (int $min, int $max) function is used to generate random integers, where $min – $max the range of ASCII code, here take 33-126, you can adjust the range as necessary, such as the ASCII code table 97– 122 digits corresponding to the a–z of the English alphabet, specific reference to the ASCII code table; the Chr (int $ascii) function is used to convert the corresponding integer $ascii to the corresponding character.

The code is as follows Copy Code

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, pass length parameter $pw_length = 6
Echo Create_password (6);

Method Two:

1. Preset a string $chars, including a–z,a–z,0–9, and some special characters

2. Randomly take a character in a $chars string

3, repeat the second step n times, can get the length of the password of n

copy code

function Generate_password ($length = 8) {
Password character set, you can add any character you need
$chars = ' abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&* ()-_ [ ]{}<>~ ' +=,.;:/?| ';

$password = ';
for ($i = 0; $i < $length; $i + +)
{
//provides two ways to get a character here
//The first is to use SUBSTR to intercept any one character in the $chars;
//The second is the word-taking Any element
//$password. = substr ($chars, Mt_rand (0, strlen ($chars)-1), 1),
$password. = $chars [Mt_rand (0 , strlen ($chars)-1)];
}

return $password;
}

Using PHP to develop applications, especially Web applications, often requires the generation of random passwords, such as user registration to generate random passwords, user reset password also need to generate a random password. Random password is a string of fixed length, here I collected several methods to generate random strings, for your reference.

Method One:

1, generate a random integer in the 33–126, such as 35,

2, convert 35 to the corresponding ASCII character, such as 35 corresponding #

3, repeat the above 1, 2 steps n times, connected to n-bit password

The algorithm mainly uses two functions, Mt_rand (int $min, int $max) function is used to generate random integers, where $min – $max the range of ASCII code, here take 33-126, you can adjust the range as necessary, such as the ASCII code table 97– 122 digits corresponding to the a–z of the English alphabet, specific reference to the ASCII code table; the Chr (int $ascii) function is used to convert the corresponding integer $ascii to the corresponding character.

The code is as follows Copy Code

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, pass length parameter $pw_length = 6
Echo Create_password (6);

Method Two:

1. Preset a string $chars, including a–z,a–z,0–9, and some special characters

2. Randomly take a character in a $chars string

3, repeat the second step n times, can get the length of the password of n

copy code

function Generate_password ($length = 8) {
Password character set, you can add any character you need
$chars = ' abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&* ()-_ [ ]{}<>~ ' +=,.;:/?| ';

$password = ';
for ($i = 0; $i < $length; $i + +)
{
//provides two ways to get a character here
//The first is to use SUBSTR to intercept any one character in the $chars;
//The second is the word-taking Any element
//$password. = substr ($chars, Mt_rand (0, strlen ($chars)-1), 1),
$password. = $chars [Mt_rand (0 , strlen ($chars)-1)];
}

return $password;
}

http://www.bkjia.com/PHPjc/629672.html www.bkjia.com true http://www.bkjia.com/PHPjc/629672.html techarticle random passwords are simple, but there are a lot of places to follow, all made up of special strings, let's look at the following three ways. Method One: 1, in 33 126 in the generation of a ...

  • 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.