Php random password generation function (four models)

Source: Internet
Author: User
The following provides four php functions for generating random passwords. The simple and practical method is a user-defined encryption function, which is difficult to crack if you do not know your encryption algorithm. Method 1: Copy the code as follows...

The following provides four php functions for generating random passwords. The simple and practical method is a user-defined encryption function, which is difficult to crack if you do not know your encryption algorithm.

Method 1

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

Method 2

Generate 6-digit, letter, and hybrid passwords

The code is as follows:
$ Str = "0123456789 abcdefghijklmnopqrstuvwxyz"; // output character set
$ N = 6; // output string length
$ Len = strlen ($ str)-1;
For ($ j = 0; $ j <200; $ j ++ ){
For ($ I = 0; $ I <$ n; $ I ++ ){
$ S. = $ str [rand (0, $ len)];
}
Echo $ s ."
";
$ S = "";
}
?>
Automatically Generate passwords for numbers, letters, and symbols
$ A = "12345678 ";
$ B = "abcdefghijklmnopqistuvwxyz ";
$ S = substr (str_shuffle ($ a), 0, 2 );
$ E = substr (str_shuffle ($ B), 0, 2 );
Echo $ s. substr (str_shuffle ("! @ # $ % ^ & * "), 0, 2). $ e;
?>

Method 3

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:

Function getmicrotime ()
{
List ($ usec, $ sec) = explode ("", microtime ());
Return (float) $ usec + (float) $ sec );
}

// Record start time
$ Time_start = getmicrotime ();

// Put the php code to be executed here, for example:
// Echo create_password (6 );

// Record end time
$ Time_end = getmicrotime ();
$ Time = $ time_end-$ time_start;

// Output the total running time
Echo "execution time $ time seconds ";
?>



Address:

Reprinted at will, but please attach the article address :-)

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.