当电子邮件和密码不匹配或电子邮件不存在时,登录表单显示消息

时间:2012-11-13 19:20:30

标签: php login user-input verify

我有一个登录表单,当密码输入错误但电子邮件不是时,会显示一条消息。但是,当电子邮件或两者都不对时,它不会显示任何内容。我想让它在两个缺失的案例中显示消息(“密码和电子邮件不匹配”)。我真的很赞赏任何帮助。

  <div id="container">
            <div>
                <h1>Log In</h1>
                <form method="POST" action="logIn.php">
                    <label>Email</label>
                    <input name= "txtEmail"/>
                    <label>Password</label>
                    <input type="password" name="txtPass"/>
                    <input type= "submit" name= "submit" value="Log In"/>
                </form>
            </div>  
        </div>

<?php

    require ('connection.php');

            if (isset($_POST["txtEmail"]))
            {
            $email= $_POST["txtEmail"];//NO RECONOCE LOS TEXT FIELDS!!
            }
            else
            {$email= "";
            echo "Email empty";}

            if (!empty($_POST['txtPass']))
            {
            $pass= $_POST['txtPass'];
            }
            else
            {$pass= "";
            echo "Password empty.";}

            // Enviar consulta
              $instruction = "SELECT Password, Email FROM customer WHERE Email = '". $email . "'";
              $query = mysql_query ($instruction, $connection)
                 or die ("Fallo en la consulta");       
           // Mostrar resultados de la consulta
              $nrows = mysql_num_rows ($query);
              if ($nrows > 0)
              {
                    $result = mysql_fetch_array ($query);
                    if (isset($result['Password']))
                    {
                    $clave = $result['Password'];
                    //echo "Isset password works.";
                    }
                    else {$result= "";}
                    Email doesnt exist, or ;}


                    if ($clave == $pass){
                        header('Location: welcomeLogIn.php');
                    }
                    else{
                        echo "Password or Email doesnt match.";
                    }
              }
    mysql_close($connection);
    ?>

2 个答案:

答案 0 :(得分:0)

感谢您提供我们的代码供我们审核。根据代码我可以看到你刚开始学习PHP。我已经用一种非常简单的方式更新了你的代码。基本上,如果电子邮件或密码为空,则将$ empty_value变量设置为1.然后在脚本结束时检查$ empty_value变量是否等于1.如果是您想要的消息将是打印。请注意,您的代码存在许多安全问题,我建议您阅读以下指南中的所有信息:http://phpsec.org/projects/guide/

<?php

    require ('connection.php');
$empty_value = 0;

            if (isset($_POST["txtEmail"]))
            {
            $email= $_POST["txtEmail"];//NO RECONOCE LOS TEXT FIELDS!!
            }
            else
            {$email= "";
            $empty_value = 1;}

            if (!empty($_POST['txtPass']))
            {
            $pass= $_POST['txtPass'];
            }
            else
            {$pass= "";
            $empty_value = 1;}

            // Enviar consulta
              $instruction = "SELECT Password, Email FROM customer WHERE Email = '". $email . "'";
              $query = mysql_query ($instruction, $connection)
                 or die ("Fallo en la consulta");       
           // Mostrar resultados de la consulta
              $nrows = mysql_num_rows ($query);
              if ($nrows > 0)
              {
                    $result = mysql_fetch_array ($query);
                    if (isset($result['Password']))
                    {
                    $clave = $result['Password'];
                    //echo "Isset password works.";
                    }
                    else {$result= "";}
                    Email doesnt exist, or ;}


                    if ($clave == $pass){
                        header('Location: welcomeLogIn.php');
                    }
                    else{
                        echo "Password or Email doesnt match.";
                    }
              }
    mysql_close($connection);
    ?>

<?php
if ($empty_value == 1) {
echo "Password or Email doesn't match.";
}
?>

  <div id="container">
            <div>
                <h1>Log In</h1>
                <form method="POST" action="logIn.php">
                    <label>Email</label>
                    <input name= "txtEmail"/>
                    <label>Password</label>
                    <input type="password" name="txtPass"/>
                    <input type= "submit" name= "submit" value="Log In"/>
                </form>
            </div>  
        </div>

答案 1 :(得分:0)

如果没有匹配的电子邮件,那么您将无法获得任何结果,因此您可以简单地添加:

if ($nrows > 0)
{
   //You current code to check if the password is correct                 
}
else{
  echo "No matching email found";
}