phdphd Posted September 23, 2013 Share Posted September 23, 2013 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! Quote Link to comment https://forums.phpfreaks.com/topic/282394-session-variables-being-partially-lost-after-redirect/ Share on other sites More sharing options...
requinix Posted September 23, 2013 Share Posted September 23, 2013 Try calling session_write_close before the script exits. Quote Link to comment https://forums.phpfreaks.com/topic/282394-session-variables-being-partially-lost-after-redirect/#findComment-1450935 Share on other sites More sharing options...
phdphd Posted September 23, 2013 Author Share Posted September 23, 2013 Even with session_write_close(); inserted before the "header('Location: page.php');" statement, the session variables still get lost. Quote Link to comment https://forums.phpfreaks.com/topic/282394-session-variables-being-partially-lost-after-redirect/#findComment-1450939 Share on other sites More sharing options...
Ch0cu3r Posted September 23, 2013 Share Posted September 23, 2013 How are you setting sessions? How are you getting session vars? Quote Link to comment https://forums.phpfreaks.com/topic/282394-session-variables-being-partially-lost-after-redirect/#findComment-1450941 Share on other sites More sharing options...
phdphd Posted September 23, 2013 Author Share Posted September 23, 2013 To set them, $_SESSION['var_name']=var_value; To check them, echo '<pre>'; print_r($_SESSION); echo '</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/282394-session-variables-being-partially-lost-after-redirect/#findComment-1450943 Share on other sites More sharing options...
DavidAM Posted September 23, 2013 Share Posted September 23, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/282394-session-variables-being-partially-lost-after-redirect/#findComment-1450945 Share on other sites More sharing options...
Solution phdphd Posted September 23, 2013 Author Solution Share Posted September 23, 2013 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! Quote Link to comment https://forums.phpfreaks.com/topic/282394-session-variables-being-partially-lost-after-redirect/#findComment-1450953 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.