jeromeramone Posted July 29, 2007 Share Posted July 29, 2007 Hi, I'm new to PHP but have been coding in ASP for 8 years so my grasp of server-side scripting is pretty solid... Have picked up php effortlessly until this session problem. I have two files that very simply illustrate my very basic problem: 1st file: <?php session_start(); $_SESSION['foobar'] = "hello"; echo("foobar: ".$_SESSION['foobar']); ?> this file returns: foobar: 1 2nd file: <?php echo("foobar: ".$_SESSION['foobar']); ?> this file returns: foobar: ...nothing's there! I've been through all the php.net documentation with no luck. I've tried "session_write_close();" after setting the variable, I've tried making the session variables "global", to no avail. The PHP version that is installed on the server is 4.3.10... is that way outta date? Thanks in advance for any insight..! Quote Link to comment Share on other sites More sharing options...
yarnold Posted July 29, 2007 Share Posted July 29, 2007 In the second file, add "session_start()" on the line before you try to echo out the session variable. session_start() needs to be exected at the top of ALL pages that call or set session variables. Quote Link to comment Share on other sites More sharing options...
Humpty Posted July 29, 2007 Share Posted July 29, 2007 $_SESSION['foobar'] = "hello"; echo("foobar: ".$_SESSION['foobar']); Perhaps try using double quotes: $_SESSION["foobar"] I've always used them and not had a problem as of yet. I did copy and paste your code and didn't get "1" as the echo. I got "hello" Sorry can't help you there any more. And also don't forget what yarnold said. Quote Link to comment Share on other sites More sharing options...
jeromeramone Posted July 29, 2007 Author Share Posted July 29, 2007 whoops, yeah, it returned "foobar: hello" not "foobar: 1" ...that was a typo. BUT, adding "session_start();" to the page worked!! I guess I assumed that the "start" in "session_start" meant that you were "starting the session"... not "starting to access the session"... seems kinda counter-intuitive doesn't it? anyways... THANKS yarnold! Quote Link to comment Share on other sites More sharing options...
Humpty Posted July 29, 2007 Share Posted July 29, 2007 I thought it was odd, but gave the benifit of the doubt, and actually thought that you had 2 problems, (one resolved by yarnold). In that case also make sure that you use session_start(); as the first PHP line and don't output anything prior to it. And also if you are gonna have the session making/reading code as an include you have to put session_start() in the 'parent file' some things we just find out the hard way hehe Quote Link to comment 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.