验证条件

Validation conditions

我想创建一个条件,如果值匹配,则显示成功消息,然后将用户重定向到索引页面,如果值不匹配,我想留在当前页面并显示错误留言。

login.php

 <?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>";
            }
        }
    ?>

configure.php

<?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;
}?>

目前它所做的只是将我重定向到索引页面,有人可以告诉我哪里出错了吗?

isset() 将始终为真,因为您设置了两次。相反,检查变量本身。

if ($success)