Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/255.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP->;REGEX |密码验证不起作用_Php_Regex_Validation_Passwords - Fatal编程技术网

PHP->;REGEX |密码验证不起作用

PHP->;REGEX |密码验证不起作用,php,regex,validation,passwords,Php,Regex,Validation,Passwords,我使用这种(几乎)复杂的密码验证: function is_password($password) { return preg_match("^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,20}$", $password); } 因此,它必须包含: 0-9之间的一位数字 一个小写字符 一个大写字符 至少6个字符 最多20个字符 这似乎不起作用。每当我键入某些字符时,如e,t,o,j,c和b;如果允许的长度正确,则函数返回true。所以大写和数字没有被验证 我

我使用这种(几乎)复杂的密码验证:

function is_password($password) {
    return preg_match("^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,20}$", $password);
}
因此,它必须包含:

  • 0-9之间的一位数字
  • 一个小写字符
  • 一个大写字符
  • 至少6个字符
  • 最多20个字符
这似乎不起作用。每当我键入某些字符时,如
e
t
o
j
c
b
;如果允许的长度正确,则函数返回true。所以大写和数字没有被验证

我做错了什么?

你忘了使用。请改用此代码:

return preg_match("/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,20}$/", $password);
或者您可以拆分每个条件并使用以下代码:

return mb_strlen($password)>=6 && mb_strlen($password)<=20
       && preg_match("/[a-z]+/", $password) && preg_match("/[A-Z]+/", $password)
       && preg_match("/[0-9]+/", $password);

return mb_strlen($password)>=6&&mb_strlen($password)我会将其拆分为多个检查。这将允许您智能地向用户(或日志)反馈密码选择失败的原因

<?php
function is_password($password){
    if (strlen($password) < 6) {
        // too short
        return false;
    }
    if (strlen($password) > 20) {
        // too long
        return false;
    }
    if (!preg_match("/[A-Z]/", $password) {
        // no upper
        return false;
    }
    if (!preg_match("/[a-z]/", $password) {
        // no lower
        return false;
    }
    if (!preg_match("/[0-9]/", $password) {
        // no digit
        return false;
    }
    return true;
}
?>

这种方法还允许您在将来根据需要轻松添加或删除限制。永远不要限制用户可以输入的密码的最大长度。1.没有充分的理由。2.这非常强烈地表明,您正在将它们存储为纯文本,您也不应该这样做。@Sammitch非常同意;密码验证的唯一限制应该是它是否足够长。我不想在我的密码中使用数字、字母、手语和音效,我永远不应该这样做。