Php generate random password program code _ PHP Tutorial

Source: Internet
Author: User
Php generates random password program code. There are many methods to generate a random password. The simplest is to use the phpmt_rand () function to directly generate a string of numbers, next, I will introduce you to the php random password generation program. There are many simple methods to generate random passwords. The simplest is to use php mt_rand () function to generate a string of numbers directly. next I will introduce you to the php random password generation program.

The simplest method is the mt_rand function.

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

Example

In this example, we will return some random numbers:

The code is as follows:

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

The output is similar:

3150906288
513289678
35

The above is relatively low in the Security Index, because it is all numbers.

1. preset a string $ chars, including A-z, a-Z, 0-9, and some special characters
2. random character in $ chars string

The code is as follows:

Function generate_password ($ length = 8 ){
// 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;
}

Because of the md5 () function returned value provided by a friend, the generated password only contains letters and numbers, but it is also a good method. Algorithm idea:
1. time () gets the current Unix timestamp
2. encrypt the timestamp obtained in step 1 with md5 ()
3. extract the data encrypted in step 2 to obtain the desired password.

The code is as follows:

Function get_password ($ length = 8)
{
$ Str = substr (md5 (time (), 0, 6 );
Return $ str;
}

The mt_rand () function directly generates a string of numbers. next I will introduce you to the simplest php program for generating random passwords...

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.