首页 > 解决方案 > 密码匹配问题

问题描述

以下是我的php代码。我正在尝试对注册进行表单验证,当我提交表单而不填写所有字段时,一切似乎都很好。当我尝试填写所有内容但故意不匹配密码时,它仍然允许我成功注册。

    if (isset($_POST['submitted']))
    {
        if (empty($_POST['useremail']))
            $newemailerr = "Email is required!";
        else 
            $newemail = ($_POST['useremail']);

        if (empty($_POST['userid']))
            $newiderr = "Username is required!";
        else 
            $newid = ($_POST['userid']);

        if (empty($_POST['userpass']))
            $newpasserr = "Password is required!";
        else 
            $newpass = ($_POST['userpass']);

        if (empty($_POST['confirmpass'] && $_POST['confirmpass'] != $_POST['userpass']))
            $newconfirmpasserr = "Both passwords do not match!";
        else 
            $newconfirmpass = ($_POST['confirmpass']);
    }

标签: phphtmlvalidation

解决方案


if (isset($_POST['submitted']))
{
    if (empty($_POST['useremail']))
        $newemailerr = "Email is required!";
    else 
        $newemail = ($_POST['useremail']);

    if (empty($_POST['userid']))
        $newiderr = "Username is required!";
    else 
        $newid = ($_POST['userid']);

    if (empty($_POST['userpass']))
        $newpasserr = "Password is required!";
    else 
        $newpass = ($_POST['userpass']);

  if (empty($_POST['confirmpass']) || $_POST['confirmpass'] != $_POST['userpass'])            $newconfirmpasserr = "Both passwords do not match!";
    else 
        $newconfirmpass = ($_POST['confirmpass']);
}

你应该在这里使用 OR

 if (empty($_POST['confirmpass']) || $_POST['confirmpass'] != $_POST['userpass'])

推荐阅读