用户登录失败时,登录表单下方未显示错误消息


Error message not showing below the login form when a user unsuccessfully log in

我是php的新手。我正试图将php添加到我的网站,以检查登录信息是否与存储在我的数据库中的信息匹配,但由于某种原因,当电子邮件地址和密码不匹配时,登录表单下方不会出现错误消息。这是我的登录表单account.php:

<!--loginform-->
<p style="margin-left:30px; font-size:23px; margin-top:150px; font-family:Gisha;">Login or Create an Account</p>
<hr style="color:#666; width:95%; align:center; margin-top:-21px;">
<div id="center-login">
<table  cellpadding="10px" cellspacing="40px">
  <form id="form1" onSubmit="return validate()" action="checklogin.php" method="post">
    <tr>
      <td width="600px"><p style="font-family:Gisha;  font-size:19px; margin-top:-25px;">New Customers</p>
        <hr style="color:#F8F8F8; width:100%;  margin-top:-5px; "></td>
      <td width="600px"><p style="font-family:Gisha; font-size:19px; margin-top:-25px;">Registered Customers</p>
        <hr style="color:#F8F8F8; width:100%;  margin-top:-5px"></td>
    </tr>
    <tr>
      <td><p style="font-family:Gisha; font-size:16px; margin-top:-130px;">By creating an account with our store, you will be able to move through the checkout process faster, store multiple shipping addresses, view and track your orders in your account and more.</p>
        <br>
        <span>
        <input class="submit-log" style="padding:7px; background:black; color:white; font-weight:bold; font-size:19px; cursor:pointer;" type="button" value="Create an Account" onClick="window.location='newaccount.html'">
        </span></td>
      <td><p style="font-family:Gisha; font-size:16px; margin-top:-45px">If you have an account with us, please log in.</p>
        <p style="font-family:Gisha; font-size:18px; font-weight:bold;">Email Address</p>
        <p>
          <input type="text" id="username" name="email" class="input"><span id="errorIMG" style="visibility:hidden; margin-top:-5px;"><img src="images/Error.png" width="15" height="15" style=" margin-bottom:-2px;"/> <span id="errorMsg" style="color:#FF0000; font-family:Gisha;"></span></span>
        </p>
        <p style="font-family:Gisha; font-size:18px; font-weight:bold;">Password</p>
        <p>
          <input type="password" id="pwd" name="pwd"><span id="errorIMG1" style="visibility:hidden; margin-top:-5px;"><img src="images/Error.png" width="15" height="15" style=" margin-bottom:-2px;"/> <span id="errorMsg1" style="color:#FF0000; font-family:Gisha;"></span></span>
        </p>
        <span>
        <input class="submit-log" style="padding:8px; background:black; color:white; font-weight:bold; font-size:19px; cursor:pointer;" type="submit" name="login" value="Login">
        </span>
</table>
</div>
<div id="errorMsg">
<?php
if(isset($_GET['check']))
{
if($_GET['check']=="fail")
{
echo "<font color='red'><b>Wrong email/password combination</b></font>";
}
else
if($_GET['check']=="good")
{
header('Location:90210.html');
}
}
?>
</div>
</form>
<!--end of login form-->

这是checklogin.php:

<?php
session_start();
$Email=$_POST['email'];
$Pwd=$_POST['pwd'];
$con=mysql_connect("localhost","root","");
if(!$con){
die('Could not connect:' .mysql_error());
}
echo "Connected successfully.";
$database=mysql_select_db('90210store');
if(!$database){
die('<br>Could not select database:' .mysql_error());
}
echo "<br>Database successfully selected";
$result = mysql_query("SELECT * FROM login
 WHERE EmailAdd='$Email' AND Password='$Pwd' LIMIT 1") or      die('QueryFailed:'.mysql_error());
if(mysql_num_rows($result) == 1) {
    $ID= $_SESSION['ID']=1234;
    header('Location:verifyuser.php');
    exit();
}

else {
   header('Location:account.php');
   exit();
}
mysql_close($con);
?>

这是verifyser.php:

<?php
session_start();
if(isset($_SESSION['ID']))
{
echo "<script>alert('Login successful!');</script>";
echo "<script>window.location = '90210.html';</script>";
exit();
}
else
{
header('Location:account.php');
exit();
}
?>

为什么它没有显示?请尝试解决此问题。非常感谢。

在不知道原因的情况下,通过阅读您描述的症状,很明显,变量$_GET['check']很可能根本没有设置。也可能是在登录尝试失败的情况下没有设置它,或者它的值与"良好"或"失败"不同。但由于您没有发布验证代码,我们只能猜测

此外,php代码容易出错。我建议你把它改成这样的结构:

<?php
if (   isset($_GET['check']) {
    && $_GET['check']=="good") ) {
        header('Location:90210.html');
} else {
    echo "<font color='red'><b>Wrong email/password combination</b></font>";
}
?>
  1. 总是测试好的情况并处理它,将其他都视为坏的情况,这样你就可以自动处理意外的情况。

  2. 你结合了两个if条件,但没有其他条件。这意味着,如果没有一个条件被评估为true,那么什么都不会发生。这不是你想要的。

  3. 我建议使用布尔AND运算符(&&)将两个检查(该变量的存在性和值)合并为一个检查。这样你就有了明确的区别:要么一切都好,要么有些事情不好,在这种情况下,你很可能会阻止访问。这样一来,您的代码就不那么复杂,因此更健壮。

一些一般性意见:

  • 您似乎将用户密码以明文形式存储在数据库!这是一种非常糟糕的风格,为您和用户带来了安全风险。相反,你应该永远不要存储真正的密码,而只存储它的哈希。然后在登录尝试中,你再次对提供的密码进行哈希,并比较哈希。这样,即使您的系统受到威胁,也可以防止密码泄露。还要记住,散列不是加密,你想在这里使用散列。

  • 您正在使用一个不推荐使用的数据库驱动程序,php的旧mysql扩展。请查阅有关这方面的优秀文档。您肯定应该切换到更现代、更健壮的新mysqli扩展或PDO,并了解"准备好的语句"的优势。您当前的代码对sql注入是完全开放的。