TheCarver
TheCarver

Reputation: 19713

Checking a session variable from an include file - PHP

When a user logs in to my system, I have a session variable $_SESSION['logged_in'] = 1; and then on each page I check the variable to see if the user is logged in or not, which works okay:

session_start();
if (!$_SESSION['logged_in']) {
   header("location: https://mydomain.com/cpanel/login.php");
   exit();
}

But now, for ease of maintenance, I want to add this session checker to an include file and include it at the top of every secure page. However, each time I log in it fails.

This is what is in the include file:

// start session
session_start();

// check login session
if (!$_SESSION['logged_in']) {
   header("location: https://mydomain.com/cpanel/login.php");
   exit();
}

And this is what I have added to the header of each page:

// load authentication file
include "../includes/authentication/check.php";

But it appears that the $_SESSION is empty, even though I am definitely logged in. I tried removing the session_start(); from the include file as I thought that might be reetting it somehow - but that wasn't it.

Have any of you guys got a clue as to what is going wrong here?

Upvotes: 0

Views: 2709

Answers (1)

mohammad mohsenipur
mohammad mohsenipur

Reputation: 3149

use include_once because with include every time include session_start(); become run I Think Problem is this

Upvotes: 2

Related Questions