Custom function code to generate random passwords in php_PHP tutorial

WBOY
Release: 2016-07-13 10:25:58
Original
743 people have browsed it

Code 1:

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

Copy code The code is as follows:

/*
* php automatically generates a new password Custom function (with example demonstration)
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 )
{
$password.=$validchars[mt_rand(0,$max_char)]; ;
}
echo "New password: ".genPassword()."
";
echo "New password: ".genPassword(5,10)."
";
?>


Some examples are summarized below for your reference.

Example 1

The simplest generation method

Copy code The code is as follows:
function generatePassword($length=8)
{
$chars = array_merge(range(0,9),
                                                                                                                                                                                                                                         . '$','%','^','&','*'));
shuffle($chars);
$password = '';
for($i=0; $i<8; $i++) {
$password .= $chars[$i];
}
return $password;
}



Example 2
1. Generate a random integer from 33 – 126, such as 35,

2. Convert 35 into the corresponding ASCII code character, such as 35 corresponding to #

3. Repeat the above steps 1 and 2 n times , concatenated into an n-digit password



Copy code

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


Examples

Copy code

The code is as follows:

mt_srand((double) microtime() * 1000000);

function gen_random_password($password_length = 32, $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;
}

?>


php 密码生成器 v 4.0



密码生成器v4.0 by freemouse



if (isset($_GET['password_length'])){
 if(preg_match("/([0-9]{1,8})/", $_GET['password_length'])){
  print("密码生成成功:

" . gen_random_password($_GET['password_length']) . "

n");
 } else {
  print("密码长度不正确!

n");
 }
}

print <<< end
请为密码生成其指定生成密码的长度:



 
 

end;

?>


例4


1、预置一个的字符串 $chars ,包括 a – z,A – Z,0 – 9,以及一些特殊字符
2、在 $chars 字符串中随机取一个字符
3、重复第二步 n 次,可得长度为 n 的密码

复制代码 代码如下:

function generate_password( $length = 8 ) {
    // 密码字符集,可任意添加你需要的字符
    $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_ []{}<>~`+=,.;:/?|';

    $password = '';
    for ( $i = 0; $i < $length; $i++ )
    {
        // 这里提供两种字符获取方式
        // 第一种是使用 substr 截取$chars中的任意一位字符;
        // 第二种是取字符数组 $chars 的任意元素
        // $password .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        $password .= $chars[ mt_rand(0, strlen($chars) - 1) ];
    }

    return $password;
}

上面经过测试性能都不如下面这个

1、预置一个的字符数组 $chars ,包括 a – z,A – Z,0 – 9,以及一些特殊字符
2、通过array_rand()从数组 $chars 中随机选出 $length 个元素
3、根据已获取的键名数组 $keys,从数组 $chars 取出字符拼接字符串。该方法的缺点是相同的字符不会重复取。

复制代码 代码如下:

function make_password( $length = 8 )
{
// Password character set, you can add any characters 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 select $length array element key names in $chars
$keys = array_rand($chars, $length);

$password = '';
for($i = 0; $i < $length; $i++)
{
// Concatenate $length array elements into a string
       $password .= $chars[$keys[$i]];
 }

return $password;
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/824893.htmlTechArticleCode 1: Function to generate a random password. The generated password is a random string of lowercase letters and numbers, with a length of Customizable. Relatively speaking, this is relatively simple. Copy code Code...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template