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

PHP generates a secure and random password program

高洛峰
高洛峰Original
2016-11-29 15:18:251002browse

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.

php generates a safe and random password program example code as follows:

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

function getRandPass($length = 6){

$password = '';

//Will Add the characters you want to the string below, the default is numbers 0-9 and 26 English letters

$chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

$char_len = strlen( $chars);

for($i=0;$i<$length;$i++){

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

//Treat this string as a Array, randomly pick out a character and loop it into the number of digits you need

$password .= $chars[$loop];

}

return $password;

}

echo getRandPass(12); //Randomly generate a 12-digit password

//Open source code phpfensi.com

?>

Example 2, a bit like the first one

1. Preset one The string $chars, including a – z, A – Z, 0 – 9, and some special characters

2. Randomly pick a character in the $chars string

3. Repeat the second step n times, you can get The example code for a password of length n is as follows:

function generate_password( $length = 8) {

// Password character set, you can add any characters you need

$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRST UVWXYZ0123456789!@#$%^ &*()-_ []{}<>~`+=,.;:/?|';

​ $password = '';

for ( $i = 0; $i < $length; $i++ )

                                                                                                                                                                                                  . Any element of the array $chars

                                                                                                                                                                                                                                                   . $chars) - 1) ];

}

return $password;

}

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