简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM