Issues with sessions not storing

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
PHPchong
Forum Newbie
Posts: 3
Joined: Sat Mar 20, 2010 11:52 pm

Issues with sessions not storing

Post 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!
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Issues with sessions not storing

Post 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.
(#10850)
PHPchong
Forum Newbie
Posts: 3
Joined: Sat Mar 20, 2010 11:52 pm

Re: Issues with sessions not storing

Post 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?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Issues with sessions not storing

Post 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.
(#10850)
Post Reply