Jump to content

Sessions and Included Files


doubledee

Recommended Posts

Does a Session's scope carry over to included files?

 

Let's say I have a file "index.php" and it has a Session.

 

If "index.php" includes a file called "header.inc.php", does the scope of the Session in "index.php" carry over to the included header?

 

For instance, could I check $_SESSION['LoggedIn'] in "header.inc.php"?

 

 

Debbie

 

Link to comment
https://forums.phpfreaks.com/topic/245656-sessions-and-included-files/
Share on other sites

Does a Session's scope carry over to included files?

 

Yes.

 

So if you had for example...

<?php
// index.php

session_start();
$_SESSION['name'] = 'Debbie';

include('header.inc.php');
?>

<?php
// header.inc.php
echo $_SESSION['name'];
?>

 

That would output:

Debbie

Correct. As long as there is session_start() at the top of the top level file(such as index.php which includes other files) then the session is active across all the included files.

 

Does that mean I need a session_start() at the top of my include files as well (e.g. "header.inc.php")??

 

 

Debbie

 

 

Does that mean I need a session_start() at the top of my include files as well (e.g. "header.inc.php")??

No. Any files that are included within a file that has Session_start() before any of the includes, will have access to the session.

Does that mean I need a session_start() at the top of my include files as well (e.g. "header.inc.php")??

No. Any files that are included within a file that has Session_start() before any of the includes, will have access to the session.

 

So what is a good strategy for working with Sessions in my website?

 

1.) Manually type:

 

<?php start_session(); ?>

at the top of every file?

 

 

2.) Include a Session file to save maybe 1 line of extra code per file?

 

 

3.) Other?

 

 

 

Debbie

 

 

 

depends on how you have your website setup. If you have index.php selecting what pages to be 'included', Then just session_start() at the top of index.php would do the trick.

 

something like...

<?php
// index.php
session_start(); // start the session

include_once('header.inc.php');

if(isset($_GET['page'])) { // if a page has been specified, try and include it
  $page = trim($_GET['page']);
  $page = 'lib'.$page.'.php'; // store pages in 'lib' folder or something similar
  if(file_exists($page)) { // check the page exists
    require_once($page);
  } else {
    require_once('lib/home.php'); // if the page doesn't exist, include a default page, home page for example
  }
} else { // if a page has not been specified, include the default page
  require_once('lib/home.php');
}

include_once('footer.inc.php');
?>

by doing this, the session is accessible in header.inc.php,  footer.inc.php as well as the request page that is included.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.