当有人输入错误的用户名或密码时,我希望在页面上显示错误消息。现在它使用一个弹出框来显示消息,但我想在页面本身上显示它。我该怎么做呢?下面是我的php
<?php
if(!defined("SESSION")){
session_start();
define("SESSION", true);
}
if(isset($_GET["log_out"])){
unset($_SESSION["logged_in"]);
header('refresh: 0; url=login.php');
exit;
}
$login = true;
require "protect.php";
$logins[0]["user"] = "";
$logins[0]["pass"] = "";
$logins[0]["redirect"] = "test.php";
$logins[1]["user"] = "";
$logins[1]["pass"] = "";
$logins[1]["redirect"] = "";
$logins[2]["user"] = "";
$logins[2]["pass"] = "";
$logins[2]["redirect"] = "test.php";
// No need to edit below, except the errors
if(isset($_POST['submit'])){ //is the form submitted?
if(empty($_POST['user']) || empty($_POST['pass'])){
header("Location: login.php");
exit;
} //check for empty user name or password
$is_logged = false; //this is part of the process to see if they have a correct password or not, set to false right here to say no right pass... (will change later)
foreach($logins as $login){
$user = $_POST;
if(($user["user"] == $login["user"]) && ($user["pass"] == $login["pass"])) {
$is_logged = true;
$_SESSION["logged_in"] = array($login["redirect"], true); //now, if they do have a correct password, set the session to true, and the variable.
header("Location: ".$login["redirect"]);
exit;
}
}
if(!$is_logged){ echo '<script type="text/javascript">alert("Inncorect username or password");window.history.go(-1);</script>'; } //if none of the $logins arrays matched the input, give an error
}
?>
答案 0 :(得分:2)
您需要做的就是使用echo命令echo一些html,而不是使用Javascript。
试试这个
echo '<p>Incorrect username or password</p>'