Page 1 of 1

Issues with sessions not storing

Posted: Sun Mar 21, 2010 12:10 am
by PHPchong
Hi all,

I have an issue that's driving me crazy, perhaps someone here can be of help. I'm putting together my church's website and I recently moved the files from one hosting account to another. I had a bunch of user authentication on the site that used sessions that worked fine on the old hosting account but just stopped working once I moved all the files over to the new hosting account. After some troubleshooting, I narrowed the issue down to the session variables not saving correctly when I navigate to different pages. I've included a simple, two page snippet of code below.

-----------------PAGE 1: -------------------------

Code: Select all

 
<? 
session_start();
 
$var1 = "Chris"; 
$_SESSION["username"]=$var1;
?>
<?=$_SESSION["username"]?> is the username stored in the session.</br>
<a href="test2.php">click</a>
 
Output:
Chris is the username stored in the session.
click

As you can see from the output above, the session variable is stored and can be retrieved on the first page. So, I click on where it says click and page 2 looks like below:

-----------------PAGE 2: -------------------------

Code: Select all

 
<? 
session_start();
?>
<?=$_SESSION["username"]?> is the username stored in the session.</br>
 
Output:
is the username stored in the session.



At this point $_SESSION["username"] is gone. If I store something in a session, shouldn't it be available to me throughout the entire session on the site until I specifically drop or destroy the session? Does this have anything to do with settings in the php.ini? The fact that it was working under my old hosting account but not working under my new one makes me think that it might. Please help!

Re: Issues with sessions not storing

Posted: Sun Mar 21, 2010 1:57 am
by Christopher
It is possible that sessions are not working at all. Perhaps there is a configuration or permissions error.

The other thing I notice is that your scripts start on line 2. If there is any output sent before session_start() then it may not work. Even whitespace characters will cause this problem. Once there is any output the headers will be send. session_start() needs to be called before headers are sent.

Re: Issues with sessions not storing

Posted: Sun Mar 21, 2010 11:50 pm
by PHPchong
If sessions weren't working at all, wouldn't the session not even have set on the first page? And if there is a configuration or permissions error, where would I set those configurations? The .ini file comes to mind, I guess I'm not sure which variables I'd start with.

As for the whitespace, I am not outputting anything before trying to get any sessions, it just appears that way because of where I placed the

Code: Select all

tags on this forum.

Are there other things I could look at to get this working?

Re: Issues with sessions not storing

Posted: Mon Mar 22, 2010 1:30 am
by Christopher
It sounds like sessions are not working on your server. Check the logs.

And yes, if you assign to $_SESSION it will be set for that page, but not saved or reloaded.