PHP Generate random Password program code _php tutorial

Source: Internet
Author: User
There are many ways to generate random passwords, the simplest is to use the PHP Mt_rand () function to generate a series of numbers directly, let me introduce you to PHP generated random password program

The simplest way to mt_rand a function

Mt_rand () returns a random integer using the Mersenne Twister algorithm.

Example

In this case, we will return some random numbers:

The code is as follows Copy Code

Echo (Mt_rand ());
Echo (Mt_rand ());
Echo (Mt_rand (10,100));
?>

The output is similar to:

3150906288
513289678
35

The above relative safety index is very low, because all is the number Oh, the following another name

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

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 + +)
{
//Here are two ways to get a character
//The first is to use SUBSTR to intercept any one character in $chars;
//The second is the number of characters to take Any element of the group $chars
//$password. = substr ($chars, Mt_rand (0, strlen ($chars)-1), 1);
$password. = $chars [Mt_rand (0, strlen ($chars)-1)];
}
return $password;
}

A friend provides the return value of the MD5 () function, and the generated password only includes letters and numbers, but it's also a good idea. Algorithm idea:
1. Time () Gets the current Unix timestamp
2, the first step to obtain the timestamp for MD5 () encryption
3, the second step to encrypt the result, intercept n-bit is the desired password

The code is as follows Copy Code

function Get_password ($length = 8)
{
$STR = substr (MD5 (Time ()), 0, 6);
return $str;
}

http://www.bkjia.com/PHPjc/631273.html www.bkjia.com true http://www.bkjia.com/PHPjc/631273.html techarticle There are many ways to generate random passwords, the simplest is to use the PHP Mt_rand () function to directly generate a series of numbers, I would like to introduce you to PHP generation random password program The simplest to do ...

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