rah
rah

Reputation: 161

Validation conditions

I would like to create a condition where if the values match the a success message appears then redirects the user to the index page, if the values don't match I want to stay on the current page and display an error message.

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", "[email protected]");
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;
}?>

At the moment all it does is redirect me to the index page, could someone let me know where I've gone wrong, please?

Upvotes: 0

Views: 72

Answers (1)

user6702203
user6702203

Reputation:

isset() will always be true as you set it twice. Instead, check the variable itself.

if ($success)

Upvotes: 2

Related Questions