Jump to content


Photo

Session Variables Being Partially Lost After Redirect


Best Answer phdphd, 23 September 2013 - 05:04 PM

And the winner is .... David !

The warning I get is of type "Cannot modify header information - headers already sent by  (output started at C:\UwAmp\www\.....\page.php:XX) in C:\UwAmp\www\.....\page.php.php on line YY...."

At XX level, there is print_r($_SESSION);, while YY corresponds to the line where the header('Location: page.php') is located.

I solved the issue by setting output_buffering to On in the php.ini file.

 

Thank you very much David!

Go to the full post


  • Please log in to reply
6 replies to this topic

#1 phdphd

phdphd

    Advanced Member

  • Members
  • PipPipPip
  • 125 posts

Posted 23 September 2013 - 03:39 PM

Hi All,
I am facing a strange problem with session variables being partially lost after redirect.
I recently moved from a wampserver/php 5.3.5 environment to a UwAmp/php 5.4.15 environment. I am using the same php files and scripts in both environments.
In one of the scripts there are session variables being set and also a "header('Location: page.php');" statement.
Everything works perfectly in the older environment. However, in the newer one, session variables belonging to the same script as the "header('Location: page.php');" statement are lost, while all other session variables previously set in other scripts are kept.
The problem persists if I use a UwAmp/php 5.3.5 environment.
Any idea of where the problem comes from ?
Thanks!



#2 requinix

requinix

    Transforming Moderator

  • Moderators
  • 6,152 posts
  • LocationWA

Posted 23 September 2013 - 03:41 PM

Try calling session_write_close before the script exits.

#3 phdphd

phdphd

    Advanced Member

  • Members
  • PipPipPip
  • 125 posts

Posted 23 September 2013 - 03:53 PM

Even with session_write_close(); inserted before the "header('Location: page.php');" statement, the session variables still get lost.



#4 Ch0cu3r

Ch0cu3r

    Advanced Member

  • Moderators
  • 2,329 posts

Posted 23 September 2013 - 04:00 PM

How are you setting sessions? How are you getting session vars?



#5 phdphd

phdphd

    Advanced Member

  • Members
  • PipPipPip
  • 125 posts

Posted 23 September 2013 - 04:07 PM

To set them,

$_SESSION['var_name']=var_value;

To check them,

echo '<pre>';
print_r($_SESSION);
echo '</pre>';


#6 DavidAM

DavidAM

    Advanced Member

  • Gurus
  • 1,974 posts
  • LocationSpring, TX USA

Posted 23 September 2013 - 04:08 PM

It is possible that the "old" environment has output buffering turned on and the script in question has some output before the call to session_start(). If the "new" environment has output buffering turned off an error occurs with the call to session_start(), and the session is not actually started, so no new values can be assigned to the session.

Turn on error_reporting() for the development server and fix any errors (or warnings or notices) that occur.
-- I haven't lost my mind, it's backed up on tape ... somewhere!

#7 phdphd

phdphd

    Advanced Member

  • Members
  • PipPipPip
  • 125 posts

Posted 23 September 2013 - 05:04 PM   Best Answer

And the winner is .... David !

The warning I get is of type "Cannot modify header information - headers already sent by  (output started at C:\UwAmp\www\.....\page.php:XX) in C:\UwAmp\www\.....\page.php.php on line YY...."

At XX level, there is print_r($_SESSION);, while YY corresponds to the line where the header('Location: page.php') is located.

I solved the issue by setting output_buffering to On in the php.ini file.

 

Thank you very much David!






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com