Ralph Schipper
Ralph Schipper

Reputation: 741

PHP after redirect the session variable changes to false

I have created a login screen that checks if the password is correct.

After submitting the login form I get to process.php that has these lines:

if (password_verify($passwordPost, $passwordDB)) {
        $_SESSION['loged_in'] = true;
    } else {
        $_SESSION['loged_in'] = false;
    }
    # when I do a print_r on $_SESSION['loged_in'] it results true
    header('Location:  ../../admin/index.php');

The index page that checks the session (../../admin/index.php)

<?php 
    session_start();
    # when I do a print_r on $_SESSION['loged_in'] here, it results false
    if ($_SESSION['loged_in'] == false) {
        include(PATH_COMPONENTS.'login/index.php'); 
    }
    ?>

How is this possible?

Upvotes: 1

Views: 151

Answers (1)

Rizier123
Rizier123

Reputation: 59681

You have to start the session in every file! Like this:

session_start();  //most times at the top of every file

YES, you have to start the session in every file your using it!

Also for error reporting use this:

<?php
    error_reporting(E_ALL);
    ini_set("display_errors", 1);
?>

Upvotes: 1

Related Questions