Page 1 of 1

$_SESSION issue

Posted: Mon Jul 27, 2009 12:06 pm
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!

Re: $_SESSION issue

Posted: Mon Jul 27, 2009 12:49 pm
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

Re: $_SESSION issue

Posted: Tue Jul 28, 2009 3:25 am
by marty pain
Yep, each page is rendered through the same page. At the start of that page it calls session_start();

Re: $_SESSION issue

Posted: Tue Jul 28, 2009 4:28 am
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??

Re: $_SESSION issue

Posted: Tue Jul 28, 2009 8:42 am
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!

Re: $_SESSION issue

Posted: Tue Jul 28, 2009 8:51 am
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