PHP表单登录 - 发送错误消息

时间:2016-04-11 03:05:29

标签: php login

我有这行代码,如果用户名密码组合不匹配,我正在尝试发送错误消息,即使在我输入用户名和密码之前,该东西就是我访问页面时显示的消息。如何在登录失败后才显示该消息? 谢谢!

<?php
$user = $_POST['user'];
$pass = $_POST['pass'];

if(($user == "someusername") && ($pass == "somepassword"))
{
        include("../securedfile.php");
}
else{    
  $message = "Invalid User/Password - Please Try Again";
  echo "<script type='text/javascript'>alert('$message');</script>";
   }   

    if(isset ($_POST))

    {?>

            <div class="secure">
            <form method="POST" action="secure.php">
            username:<input class="user" type="text" name="user"></input><br/><br/>
           password:<input class="pass" type="password" name="pass"></input><br/><br/>


            <button class="submit" type="submit" name="submit">SUBMIT</span></button>
            </form>
            </div>
    <?}
    }    
?>

1 个答案:

答案 0 :(得分:1)

&#39; else&#39;存在括号问题,然后表单调用secure.php而不是调用自身,没有isset ...下面的重写代码会更好这样做的方式。并检查它,它的工作原理。 (整个文件的名称 - new.php)

<div class="secure">
        <form method="POST" action="new.php">
        username:<input class="user" type="text" name="user"></input><br/><br/>
        password:<input class="pass" type="password" name="pass"></input><br/><br/>


        <button class="submit" type="submit" name="submit">SUBMIT</span></button>
        </form>
</div>

<?php  
// checking the user
if(isset($_POST['submit']))
{

  $user = $_POST['user'];
  $pass = $_POST['pass'];

if($user == "someusername" && $pass == "somepassword")
  {
    include("../securedfile.php");
  }

else
 {
 $message = "Invalid User/Password - Please Try Again";
 echo "<script type='text/javascript'>alert('$message');</script>";
 }
}  
?>