Home >Backend Development >PHP Tutorial >PHP generates random password functions (four types)_PHP tutorial
The following provides four PHP functions to generate random passwords. The method is simple and practical. It is a user-defined encryption function, so it is difficult to crack if you don’t know your encryption algorithm.
The following provides four php tutorials to generate random password functionsOh, the method is simple and practical. It is a user-defined encryption function, so it is difficult to crack if you don’t know your encryption algorithm.
Method 1
function generate_password( $length = 8 ) {
// Password character set, you can add any characters you need
$chars = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()-_ []{}<>~`+=,.;:/?|';$password = '';
for ( $i = 0; $i < $length; $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;
}
Method 2
Automatically generate a 6-digit mixed password of numbers and letters
$str = "0123456789abcdefghijklmnopqrstuvwxyz"; // Output character set
$n = 6; // Output string length
$len = strlen($str)-1;
for($j=0; $j<200; $j++){
for($i=0; $i<$n; $i++){
$s .= $str[rand(0,$len)];
}
echo $s . "
";
$s = "";
}
?>
Automatically generate passwords with numbers, letters, and symbols
$a = "12345678";
$b = "abcdefghijklmnopqistuvwxyz";
$s = substr(str_shuffle($a), 0, 2);
$e = substr(str_shuffle($b), 0, 2);
echo $s . substr(str_shuffle("!@#$%^&*"), 0, 2) . $e;
?>
Method 3
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 and pass the length parameter $pw_length = 6
echo create_password(6);
Method 3
function getmicrotime()
{
List($usec, $sec) = explode(" ",microtime());
Return ((float)$usec + (float)$sec);
}
// Recording start time
$time_start = getmicrotime();
//Put the php code to be executed here, such as:
// echo create_password(6);
// Record end time
$time_end = getmicrotime();
$time = $time_end - $time_start;// Output the total running time
echo "Execution time $time seconds";
?>