Password Strength Check Function
Login or Register to Bookmark this snippet
This function returns an array based on the password passed to it. It can determine if the password matches the optionally supplied $username.
Usage:
$strength = checkPasswordStrength('rover','user531');
Then use the values in the returned array to do stuff from there.
<?php
function checkPasswordStrength($password, $username = false) {
'strength' => 0,
'error' => 0,
'text' => ''
);
if ($length < 8) {
$returns['error'] = 1;
$returns['text'] = 'The password is not long enough';
} else {
//check for a couple of bad passwords:
$returns['error'] = 4;
$returns['text'] = 'Password cannot be the same as your Username';
$returns['error'] = 3;
$returns['text'] = 'Password is too common';
} else {
$consecutives =
count($matches[0]);
$numbers =
count($matches[0]);
$uppers =
count($matches[0]);
$others =
count($matches[0]);
//see if there are 3 consecutive chars (or more) and fail!
if ($consecutives > 0) {
$returns['error'] = 2;
$returns['text'] = 'Too many consecutive characters';
} elseif ($others > 1 || ($uppers > 1 && $numbers > 1)) {
//bulletproof
$returns['strength'] = 5;
$returns['text'] = 'Virtually Bulletproof';
} elseif (($uppers > 0 && $numbers > 0) || $length > 14) {
//very strong
$returns['strength'] = 4;
$returns['text'] = 'Very Strong';
} else if ($uppers > 0 || $numbers > 2 || $length > 9) {
//strong
$returns['strength'] = 3;
$returns['text'] = 'Strong';
} else if ($numbers > 1) {
//fair
$returns['strength'] = 2;
$returns['text'] = 'Fair';
} else {
//weak
$returns['strength'] = 1;
$returns['text'] = 'Weak';
}
}
}
return $returns;
}
?>
Added by JC on 18th December, 2007
There are no comments about this snippet.
You must be registered and logged in to post a comment.
Login here to post a comment