Jump to content

[SOLVED] can't set session variables - stumped!


jeromeramone

Recommended Posts

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..!

Link to comment
Share on other sites

$_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" :D Sorry can't help you there any more.  And also don't forget what yarnold said.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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'

 

:D

 

some things we just find out the hard way :D hehe

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.