PHP中的密码强度检查 [英] Password strength check in PHP

查看:117
本文介绍了PHP中的密码强度检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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;   
  }

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

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.

推荐答案

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);
}

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

这篇关于PHP中的密码强度检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆