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

PHP generates a secure and random password program

高洛峰
Release: 2016-11-29 15:18:25
Original
1008 people have browsed it

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;

}

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