我想创建一个条件,如果值匹配,则会显示成功消息,然后将用户重定向到索引页面,如果值不匹配,我希望保留在当前页面上并显示错误消息。
<?php
//If input set then check if user input matches store details
if(isset($_POST) and $_POST)
{
$success = false;
$email = $_POST['email'];
$pass = $_POST['pass'];
$success = login($_POST['email'],$_POST['pass']);
//If successful display alert, redirect to index page
if(isset($success))
{
$success = true;
echo "<script type='text/javascript'>alert('Login successful\nRedirecting now.')</script>";
header('Location: index.php');
die();
}
//If unsuccessful display alert
else
{
echo "<script type='text/javascript'>alert('Login failed!')</script>";
}
}
?>
<?php
//Define constants
define("EMAILAD", "hello@domain.com");
define("PASSWORD", "pwd");
//Create function to compare input to constants
function login($email, $pass)
{
$success = false;
if(($email == EMAILAD) && ($pass == PASSWORD))
{
$success = true;
}
return $success;
}?>
目前所做的一切都是将我重定向到索引页面,有人可以让我知道我哪里出错了吗?
答案 0 :(得分:2)
isset()
将始终为真。相反,检查变量本身。
if ($success)