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
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
Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.