Home  >  Article  >  Backend Development  >  PHP generates a secure and random password program_PHP tutorial

PHP generates a secure and random password program_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:48:00908browse

php generates a random password, which is convenient and fast. It can randomly generate safe and reliable passwords. I hope this article will be helpful to everyone.

Example

$chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
The code is as follows
 代码如下 复制代码

header("Content-type:text/html;charset=utf-8");

function getRandPass($length = 6){

 $password = '';

 //将你想要的字符添加到下面字符串中,默认是数字0-9和26个英文字母

 $chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

 $char_len = strlen($chars);

 for($i=0;$i

  $loop = mt_rand(0, ($char_len-1));

  //将这个字符串当作一个数组,随机取出一个字符,并循环拼接成你需要的位数

  $password .= $chars[$loop];

 }

 return $password;

}

echo getRandPass(12); //随机生成一个12位数的密码

 


?>

Copy code

header("Content-type:text/html;charset=utf-8");

function getRandPass($length = 6){


$password = '';

//Add the characters you want to the string below, the default is numbers 0-9 and 26 English letters
 代码如下 复制代码

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

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

    return $password;
}

$char_len = strlen($chars);

for($i=0;$i //Treat this string as an array, randomly pick out a character, and loop to splice it into the number of digits you need $password .= $chars[$loop]; } return $password; } echo getRandPass(12); //Randomly generate a 12-digit password

?> Where to generate password Example 2, a bit similar to the first one 1. Preset a string $chars, including a – z, A – Z, 0 – 9, and some special characters 2. Randomly pick a character from the $chars string 3. Repeat the second step n times to get a password of length n
The code is as follows Copy code
function generate_password( $length = 8 ) { // Password character set, you can add any characters you need $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_ []{}~`+=,.;:/?|'; $password = ''; for ( $i = 0; $i { //There are two ways to obtain characters // The first is to use substr to intercept any character in $chars;                                                                                                                                                                                                                              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; } http://www.bkjia.com/PHPjc/632821.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632821.htmlTechArticlephp generates a random password, which is convenient and fast. It can randomly generate safe and reliable passwords. I hope this article will be useful to everyone. Helps. Example code is as follows Copy code ?php header(Content-...
Statement:
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