验证条件

时间:2017-02-20 17:17:33

标签: php input conditional

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

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

目前所做的一切都是将我重定向到索引页面,有人可以让我知道我哪里出错了吗?

1 个答案:

答案 0 :(得分:2)

设置两次时,

isset()将始终为真。相反,检查变量本身。

if ($success)