PHP 7.1.5表单验证错误消息不一致

时间:2018-03-16 22:40:38

标签: php

我正在尝试使用其他输入字段创建注册表单,下面是仅涉及密码字段验证的代码段。一切正常,直到密码验证。碰巧的是,当我发布一个5位数或更多数字字符时,表单只会提交,但是当我发布5个字母长的密码时,它不会提交。所以它仍然回声"密码必须至少6个字符"即使我发布超过10个字母的密码。我错过了什么?

<?
if(isset($_POST['pass']) && isset($_POST['pass2'])) {

  unset($_SESSION['pass']);
    unset($_SESSION['pass2']);

  $pass = htmlentities($_POST['pass']);
  $_SESSION['pass'] = $pass;

    $pass2 = htmlentities($_POST['pass2']);
    $_SESSION['pass2'] = $pass2;

  if ($pass != $pass2) {
        $_SESSION['error'] = "Passwords did not match";
        header('Location: register1.php');
        return;
  }

    elseif(strlen($pass < 5)) {
        $_SESSION['error'] = "Password must be 6 characters at least";
        header('Location: register1.php');
        return;
    } 

    else{}
}

?>

<html>
<body>
<?    
if (isset($_SESSION['error'])) {
  echo('<p style="color:red; font-size: 0.9em;">'.$_SESSION["error"]."
  </p>\n");
  unset($_SESSION['error']);
}             
?>

  <form action="register1.php" method="post">
    <p>
    <input type="password" name="pass" placeholder="Password"  required>
    </p>
    <p>
    <input type="password" name="pass2" placeholder="Confirm Password" 
    required>
    </p>
    <p>
    <input type="submit" value="Register">
    </p>
    </form>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

您使用strlen($pass < 5)代替strlen($pass) < 5

因此strlen($pass<5)将始终返回true,因为$pass将转换为整数并且将等于0,因此,低于5(除非$pass elseif(strlen($pass) < 5) { $_SESSION['error'] = "Password must be 6 characters at least"; header('Location: register1.php'); return; } 字符串以5或更多开头,例如:&#34; 6test&#34;)。

试试这个:

Sarah <- c("Red", "Blue", "Yellow", "Green", "Pink")
Ben <- c("Black", "White", "Silver", "Pink", "Red", "Purple")
Jenny <- c("Green", "Orange", "Gold")

m_color <-unique(c(Sarah,Ben,Jenny))

TF <-data.frame(color = m_color)

TF$Sarah <- m_color%in%Sarah
TF$Ben <- m_color%in%Ben
TF$Jenny <- m_color%in%Jenny