php PHP中的密码强度检查

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10752862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:57:28  来源:igfitidea点击:

Password strength check in PHP

phpvalidationpasswords

提问by Byakugan

I am trying to create a password check script. I already have checks for email (for not allowed characters) like this:

我正在尝试创建密码检查脚本。我已经检查了电子邮件(不允许的字符),如下所示:

  public function checkEmail($email)
  {
    if (filter_var($email, FILTER_VALIDATE_EMAIL))
      return true;
    else
      return false;   
  }

So I am looking for a password validation function that checks passwords have at least one alphanumeric character, and one numeric character, and a minimum of 8 characters, and also provides error messages.

因此,我正在寻找一种密码验证功能,该功能可以检查密码是否至少包含一个字母数字字符和一个数字字符,并且至少包含 8 个字符,并且还提供错误消息。

回答by Jeroen

public function checkPassword($pwd, &$errors) {
    $errors_init = $errors;

    if (strlen($pwd) < 8) {
        $errors[] = "Password too short!";
    }

    if (!preg_match("#[0-9]+#", $pwd)) {
        $errors[] = "Password must include at least one number!";
    }

    if (!preg_match("#[a-zA-Z]+#", $pwd)) {
        $errors[] = "Password must include at least one letter!";
    }     

    return ($errors == $errors_init);
}

Edited version of this: http://www.cafewebmaster.com/check-password-strength-safety-php-and-regex

编辑版本:http: //www.cafewebmaster.com/check-password-strength-safety-php-and-regex