Custom function code for generating random passwords in PHP _php tips

Source: Internet
Author: User
Tags generator strlen

Code One:

A function that generates a random password, which is a random string of lowercase letters and numbers, and can be customized in length. Relatively speaking, this is relatively simple

Copy Code code as follows:

<?php
/*
 * PHP automatically generates a new password custom function (demo with instance)
       applicable environment: php5.2.x /mysql 5.0.x
* */
Function Genpassword ($min = 5, $max = 8)  
{  ;
    $validchars = "abcdefghijklmnopqrstuvwxyz123456789"; 
    $max _char= Strlen ($validchars) -1; 
    $length =mt_rand ($min, $max); 
    $ Password = ""; 
    for ($i =0; $i < $length; $i)  
    { 
&nb sp;       $password. = $validchars [Mt_rand (0, $max _char)]; 
    } 
        return $password; 
   } 
    echo "New password:". Genpassword (). <br> "; 
    echo" New password: ". Genpassword (5,10)." <br> ";
?>


The following summary of some examples of friends can refer to.

Example 1

The most concise way to build

Copy Code code as follows:

function Generatepassword ($length =8)
{
$chars = Array_merge (range (0,9),
Range (' A ', ' Z '),
Range (' A ', ' Z '),
Array ('! ', ' @ ', ' $ ', '% ', ' ^ ', ' & ', ' * '));
Shuffle ($chars);
$password = ';
For ($i =0 $i <8; $i + +) {
$password. = $chars [$i];
}
return $password;
}

Example 2

1. Generate a random integer, such as 35, in 33–126
2, convert 35 to the corresponding ASCII code characters, such as 35 corresponding #
3, repeat the above 1, 2 steps n times, connected to the N-bit password

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

Instance

Copy Code code as follows:

<?php
Mt_srand (Double) microtime () * 1000000);

function Gen_random_password ($password _length = $generated _password = "") {
$valid _characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$chars _length = strlen ($valid _characters)-1;
for ($i = $password _length; $i-;) {
$generated _password. = $valid _characters[mt_rand (0, $chars _length)];

$generated _password. = substr ($valid _characters, (Mt_rand ()% (strlen ($valid _characters)), 1);
}
return $generated _password;
}

? ><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<title>php Password Generator v 4.0</title>
<style type= "Text/css" >
Body {
font-family:arial;
font-size:10pt;
}
</style>
<body>
<span style= "Font-weight:bold; font-size:15pt; " > Password Generator v4.0 by freemouse</span><br/><br/>
<?php

if (Isset ($_get[' password_length ')) {
if (Preg_match ("/([0-9]{1,8})/", $_get[' Password_length ')) {
Print ("Password generation succeeded: <br/>
<span style= "Font-weight:bold" > ". Gen_random_password ($_get[' password_length ')). "</span><br/><br/>n");
} else {
Print ("Incorrect password length!<br/><br/>n");
}
}

Print <<< End
Please generate the length of its specified build password for the password: <br/><br/>
<form action= "{$_server[' php_self ']}" method= "get" >
<input type= "text" name= "Password_length" >
<input type= "Submit" value= "Build" >
</form>
End

?>
</body>

Example 4


1, preset a string $chars, including A–z,a–z,0–9, as well as some special characters
2. Randomly take a character in a $chars string
3, repeat the second step n times, can get a length of n password

Copy Code code as follows:

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 the $chars;
The second is to take any element $chars the character array
$password. = substr ($chars, Mt_rand (0, strlen ($chars)-1), 1);
$password. = $chars [Mt_rand (0, strlen ($chars)-1)];
}

return $password;
}

The test performance is not as good as the following

1, preset a character array $chars, including a–z,a–z,0–9, as well as some special characters
2. Randomly select $length elements from the array $chars by Array_rand ()
3, according to the acquired key array group $keys, from the array $chars out character stitching string. The disadvantage of this method is that the same characters are not repeated.

Copy Code code as follows:

function Make_password ($length = 8)
{
Password character set, you can add any character you need
$chars = Array (' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' H ',
' I ', ' j ', ' K ', ' l ', ' m ', ' n ', ' o ', ' P ', ' Q ', ' R ', ' s ',
' t ', ' u ', ' V ', ' w ', ' x ', ' y ', ' z ', ' A ', ' B ', ' C ', ' D ',
' E ', ' F ', ' G ', ' H ', ' I ', ' J ', ' K ', ' L ', ' M ', ' N ', ' O ',
' P ', ' Q ', ' R ', ' S ', ' T ', ' U ', ' V ', ' W ', ' X ', ' Y ', ' Z ',
' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', '! ',
' @ ', ' # ', ' $ ', '% ', ' ^ ', ' & ', ' * ', ' (', ') ', '-', ' _ ',
' [', '] ', ' {', '} ', ' < ', ' > ', ' ~ ', ' ', ' + ', ' = ', ', ', ', ', ', ', '
'.', ';', ':', '/', '?', '|');

Randomly taking $length array element key name in a $chars
$keys = Array_rand ($chars, $length);

$password = ';
for ($i = 0; $i < $length; $i + +)
{
To concatenate $length array elements into strings
$password. = $chars [$keys [$i]];
}

return $password;
}

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.