BeeFriedman
BeeFriedman

Reputation: 1974

Why is my PHP page submitting a form through the page that redirected it?

I have an index.php that checks if the user is logged in, if not it redirects the user to the login page as follows:

<?php 
    session_start();
    $_SESSION["page_identifier"] = "index"; 
    include "inc/header.php"; 

    if(!isset($_SESSION["loggedin"]) OR !$_SESSION["loggedin"] == TRUE){
        echo "ERROR: Not logged in!";
        header("Refresh: 3; url = login.php");
    }
    else{
         ......
    }
?>

This is the login page.

<?php 
    $_SESSION["page_identifier"] = "login";
    $_SESSION["form_identifier"] = "login";   
    include "inc/header.php"; 
?>
    <div>
        <form action="submission.php" name="login" method="POST">
            <input class="textBox" type="text" name="username" placeholder="Enter your username" maxlength="20"/>
            <input class="textBox" type="password" name="password" placeholder="Enter your password" maxlength="20"/><br><br>
            <input class="button" type="submit" value="Submit"/>
        </form>
        <form class="register" action="register.php">
            <input class="button" type="submit" value="New user? Register"/>
        </form>
    </div>
<?php include "inc/footer.php"; ?>

Each page sets the $_SESSION["form_identifier"] to its page name, when a form is submitted I check in the submission file from where it came from as follows:

<?php
    session_start();
    $_SESSION["page_identifier"] = "submission";
    
    //checking if the form is a login form.
    if($_SESSION["form_identifier"] == "login"){
        .......
    }

It show that the from comes from "NULL" not from "login". Why does this happen, and is there a way to change this?

Upvotes: 0

Views: 42

Answers (1)

BeeFriedman
BeeFriedman

Reputation: 1974

This question was answered by ADyson in the comments, i forgot to put "session_start();" in the login page.

Upvotes: 1

Related Questions