$_SESSION issue

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
marty pain
Forum Contributor
Posts: 105
Joined: Thu Jun 11, 2009 5:32 am
Location: Essex

$_SESSION issue

Post by marty pain »

Was just writing a massive post regarding a problem but fixed it whilst writing, which was nice.

But it has left me with a question. the $_SESSION super global was not always keeping all the values I set in the previous page, almost as if $_SESSION wasn't always being used as a super global in my scripts. This wasn't a problem on our main server so guessed it was something in the PHP.ini file. The only differences where that the server had register_long_arrays set to Y and my local machine had these off. After setting register_long_arrays to Y on my local machine the problem has stopped.

However, I thought that this was only set if you used the $HTTP_*_VARS instead of the more up-to-date super globals. Does anyone have any ideas as to why this was a fix for my problem, as I'm a bit baffled.

EDIT - Problem has returned, please keep reading!
Last edited by marty pain on Tue Jul 28, 2009 4:29 am, edited 1 time in total.
spider.nick
Forum Commoner
Posts: 72
Joined: Wed Jul 15, 2009 12:22 pm
Location: Overland Park, KS

Re: $_SESSION issue

Post by spider.nick »

marty pain wrote:Was just writing a massive post regarding a problem but fixed it whilst writing, which was nice.

But it has left me with a question. the $_SESSION super global was not always keeping all the values I set in the previous page, almost as if $_SESSION wasn't always being used as a super global in my scripts. This wasn't a problem on our main server so guessed it was something in the PHP.ini file. The only differences where that the server had register_long_arrays set to Y and my local machine had these off. After setting register_long_arrays to Y on my local machine the problem has stopped.

However, I thought that this was only set if you used the $HTTP_*_VARS instead of the more up-to-date super globals. Does anyone have any ideas as to why this was a fix for my problem, as I'm a bit baffled.
You have verified that session_start() is at the top of all of the pages that you use $_SESSION?

Nick
marty pain
Forum Contributor
Posts: 105
Joined: Thu Jun 11, 2009 5:32 am
Location: Essex

Re: $_SESSION issue

Post by marty pain »

Yep, each page is rendered through the same page. At the start of that page it calls session_start();
marty pain
Forum Contributor
Posts: 105
Joined: Thu Jun 11, 2009 5:32 am
Location: Essex

Re: $_SESSION issue

Post by marty pain »

OK. Seems that the problem has come back! :crazy:

I set some session information on one page, then use a header redirection to the next page. When viewing $_SESSION as an array on the next page the information I set has reverted back to it's previous value.

Anyone got any ideas on this??
marty pain
Forum Contributor
Posts: 105
Joined: Thu Jun 11, 2009 5:32 am
Location: Essex

Re: $_SESSION issue

Post by marty pain »

OK, I think i have fixed this again.

The session was being set before redirecting the page using header(location:<url>).

Code: Select all

if ( $result ){
                            $_SESSION['pageData']['booking']['bkMode'] = "existing";
                            header("location:render.php?page=booking_bkMain");
                        }
                        else{
                            $error = $result;
                        }
Found that the session had to be saved and closed before any re-directs in order to guarantee the data is preserved, so added an extra line to do this as shown below

Code: Select all

if ( $result )
 
                        {
 
                            $_SESSION['pageData']['booking']['bkMode'] = "existing";
                            [color=#BF0000]session_write_close();[/color]
                            header("location:render.php?page=booking_bkMain");
 
                        }
 
                        else
 
                        {
 
                            $error = $result;
 
                        }
Everything seems to be as it should in the session after the redirect now.

Hope this helps someone else as it has taken me all day to find this. Just hope it stays fixed now!
spider.nick
Forum Commoner
Posts: 72
Joined: Wed Jul 15, 2009 12:22 pm
Location: Overland Park, KS

Re: $_SESSION issue

Post by spider.nick »

Perhaps, the problem exists because you do not use die() after your header() call? I am not exactly sure what it is that die() does, but I would assume it calls session_write_close().

Nick
Post Reply