doubledee Posted August 25, 2011 Share Posted August 25, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/245656-sessions-and-included-files/ Share on other sites More sharing options...
MasterACE14 Posted August 25, 2011 Share Posted August 25, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/245656-sessions-and-included-files/#findComment-1261691 Share on other sites More sharing options...
doubledee Posted August 25, 2011 Author Share Posted August 25, 2011 I suppose the idea of a Session is that it carries over into ALL PAGES as long as it is still alive, right? Debbie Quote Link to comment https://forums.phpfreaks.com/topic/245656-sessions-and-included-files/#findComment-1261692 Share on other sites More sharing options...
MasterACE14 Posted August 25, 2011 Share Posted August 25, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/245656-sessions-and-included-files/#findComment-1261694 Share on other sites More sharing options...
doubledee Posted August 25, 2011 Author Share Posted August 25, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/245656-sessions-and-included-files/#findComment-1261703 Share on other sites More sharing options...
MasterACE14 Posted August 25, 2011 Share Posted August 25, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/245656-sessions-and-included-files/#findComment-1261706 Share on other sites More sharing options...
doubledee Posted August 25, 2011 Author Share Posted August 25, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/245656-sessions-and-included-files/#findComment-1261709 Share on other sites More sharing options...
MasterACE14 Posted August 25, 2011 Share Posted August 25, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/245656-sessions-and-included-files/#findComment-1261717 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.