PHP: Test a Password Strength
function PasswordStrength($password, $return_as_number = true) {
if (strlen($password) == 0) {
return 1;
}
$strength = 0;
/* Get the length of the password */
$length = strlen($password);
/* Check if password is not all lower case */
if (strtolower($password) != $password) {
$strength += 1;
}
/* Check if password is not all upper case */
if (strtoupper($password) == $password) {
$strength += 1;
}
/* Check string length is 8-15 chars */
if ($length >= 8 && $length <= 15) {
$strength += 1;
}
/* Check if lenth is 16 - 35 chars */
if ($length >= 16 && $length <= 35) {
$strength += 2;
}
/* Check if length >35 chars */
if ($length > 35) {
$strength += 3;
}
/* Get the numbers in the password */
preg_match_all('/[0-9]/', $password, $numbers);
$strength += count($numbers[0]);
/* Check for special chars */
preg_match_all('/[|!@#$%&*\/=?,;.:\-_+~^\\\]/', $password, $specialchars);
$strength += sizeof($specialchars[0]);
/* Get the number of unique chars */
$chars = str_split($password);
$num_unique_chars = sizeof(array_unique($chars));
$strength += $num_unique_chars * 2;
/* Strength is a number 1-10 */
$strength = $strength > 99 ? 99 : $strength;
$strength = floor($strength / 10 + 1);
/* Return number OR word */
if ($return_as_number) {
return $strength;
} else {
if (($strength >= 1) && ($strength < 3))
return 'Weak';
if (($strength >= 3) && ($strength < 6))
return 'Normal';
if (($strength >= 6) && ($strength < 8))
return 'Good';
if (($strength >= 8) && ($strength <= 10))
return 'Awesome';
}
}
Комментариев нет:
Отправить комментарий