PHP简单的判断密码强度
记录一下基于PHP如何简单的判断用户提交的密码强度,是否包含大小写字母、数字、符号等。
$password = trim($_GPC['password']);
if (strlen($password) < 8) {
show_json(0, '密码长度至少8位');
}
$score = 0; //强度分数
//包含数字
if (preg_match('/[0-9]+/', $password)) {
++$score;
}
//包含小写字母
if (preg_match('/[a-z]+/', $password)) {
++$score;
}
//包含大写字母
if (preg_match('/[A-Z]+/', $password)) {
++$score;
}
//包含特殊符号
if (preg_match('/[_|\\-|+|=|*|!|@|#|$|%|^|&|(|)]+/', $password)) {
++$score;
}
if ($score < 2) {
show_json(0, '密码必须包含大小写字母、数字、标点符号的其中两项');
}