Hi guys 'n gals.. first time poster here.. Looks like a great community you have here! I have been browsing around, and have found the replies to be quite informative and friendly. I'm hoping someone could lend their expertise and helpful hand on an issue I am having. I'm slowly getting the hang of PHP, but there are still some gaps that I do not quite get yet!

I have been looking into getting a $_SESSION variable accessed from within a require_once() function..

so my main index.php starts out like such:

<?php
session_start();

if(!isset($SESSION['browser'])){
// Establish and set superglobal variable $
SESSION['browser'].
$SESSION['browser'] = $SERVER['HTTP_USER_AGENT'];
}
?>

..some more page code follows...
...then, in the middle of my page, I need to require an include file:

<?php
require_once("/include/leftNav.inc.php");
?>

Over in my leftNav.inc.php page, I need to access the $_SESSION['browser'] variable to help define the outcome of my navigation panel...

<?php
if(ereg('MSIE (([0-9]).[0-9])', $_SESSION['browser'], $matches)){
// do this setup...
} else {
//do that setup instead....
}
?>

But as it stands, I get an 'Notice: Undefined variable: _SESSION... ..blah blah blah .. .' message :glare: So I was wondering.. since $SESSION variables are supposed to be global in scope, how come I cannot simply plug them into seperate require_once() include files? What is the solution to this?

Any help would be greatly appreciated!

Cheers,

NRG

    Are we talking about HTML frames or iframes here? If so, a PHP file launched by one frame will be completely independent of that in another frame, and so would need its own session_start(), and depending on which order the browser chooses to call those files, the values set in $_SESSION by one script may not yet be saved to disk when the other script tries to access the session data.

    If we're not talking about a frames situation, then I'm not sure what the problem might be.

      Hi NogDog.. to answer your question.. there is no frames used..
      So it is basically a matter of two php pages.. one an index.php, and the other a separate page as an include.. no frames used at all in either.

      I have been reading through some of the newbie threads.. it's interesting that since $SESSION variables are global, there is no need to use 'global $SESSION;'.. But the interesting thing is that when I use this line at the top of my include page, php(5) no longer complains about an undefined session variable.. hmmm... but, while php no longer complains, the $_SESSION variable is still not being recognized.. Surely, there must be a way to pass session variables between main pages that declare them and separate include files...

      Cheers,

      NRG

        As you say, $SESSION should be a super-global array available anywhere in a script, including its includes. The only two things I can think of at the moment that would cause a problem would be if (1) you were including via a URL (which you do not appear to be), causing the included file to be processed separately; or (2) the code unset()s the $SESSION variable at some point or overwrites it (e.g.: $SESSION = array();). You may therefore want to search through all files involved here for all instances of "$SESSION" to see if there's any place where you do anything suspicious with it.

          The answer was so simple it's down right idiotic! But to address your points first, no URL passing, and no unset() functionality was used..

          Nog.. it appears I'm an idiot... I went back to the drawing board and did a very simple stripped down script and a simple stripped down include file.. but with one crucial difference.. they were both placed in the root.. and would you know it.. it worked! So the culprit in my case was a bad directory path :eek: (sometimes you try to see the forest through the trees...)

          So thanks for your input and suggestions... this was a case of user error (as it probably is for the most part).

          Cheers,

          NRG

            Write a Reply...