PHP generates random cipher functions (four sections)

Source: Internet
Author: User
Tags character set strlen

Method One

The code is as follows Copy Code

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

    $password = ';
    for ($i = 0; $i < $length; $i + +)
    {
        //Here are two character capture methods
       //The first is to use SUBSTR to intercept any one character in the $chars;
       //The second is any element that takes a character array $chars
        $password. = substr ($chars, Mt_rand (0, strlen ($chars)-1), 1);
        $password. = $chars [Mt_rand (0, strlen ($chars)-1)];
   }

    return $password;
}

Method Two

Generate 6 digit, letter mixed password

The code is as follows Copy Code
<?php
$str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 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. "<br/>";
$s = "";
}
?>
Automatically generate passwords for numbers, letters, and symbols
<?php
$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 Three

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


Method Three

  code is as follows copy code

<?php
Function Getmicrotime ()
{
    list ($usec, $sec) = Explode ("", Microtime ());
    return ((float) $usec + (float) $sec);
}
 
//Record start time
$time _start = Getmicrotime ();
   
//Here is the PHP code to execute, such as:
//EC Ho Create_password (6);
 
//Record end time
$time _end = Getmicrotime ();
$time = $time _end-$time _start;

 //output Run total time
echo "Execution time $time seconds";
?

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.