Closing a session keeps it open? Bwuh?
June 3, 2011 7:49 PM Subscribe
Why does my session stay intact if I put session_write_close() in my function, but it dies if I leave that out?
I'm using a fairly simple login system that uses sessions and building on that to make a little site for myself.
My index page is calling a function in another php file (the functions file) that takes in a static variable from the index page, does a DB lookup on it, then returns an array as the result, which the index page then parses to display information from.
Before I visit the index page, I log in through the login page, I redirect or manually go to the index page, and verify that I'm logged in. I will stay logged in and the login session will stay intact if one of the following conditions are met:
A) I echo out the "return" line at the end of the function so that the function processes, but doesn't return anything to the index page.
B) I add a session_write_close() line directly above the return line at the end of the function.
C) Both of the above.
If I leave off the session_write_close() but try to return a value from the function to the main index page, reloading the index page causes the session to die and me to have to log in again.
I'm using PHP5, the session is only being called once, and the function does not touch the session in any way (I don't call session variables or anything from within it). Somehow, for some reason, the function returning a value kills the session, but session_write_close() keeps the session alive!
Why on earth does adding session_write_close() before I return a value in the function that I'm calling from the index page actually keep the session alive?
I'm using a fairly simple login system that uses sessions and building on that to make a little site for myself.
My index page is calling a function in another php file (the functions file) that takes in a static variable from the index page, does a DB lookup on it, then returns an array as the result, which the index page then parses to display information from.
Before I visit the index page, I log in through the login page, I redirect or manually go to the index page, and verify that I'm logged in. I will stay logged in and the login session will stay intact if one of the following conditions are met:
A) I echo out the "return" line at the end of the function so that the function processes, but doesn't return anything to the index page.
B) I add a session_write_close() line directly above the return line at the end of the function.
C) Both of the above.
If I leave off the session_write_close() but try to return a value from the function to the main index page, reloading the index page causes the session to die and me to have to log in again.
I'm using PHP5, the session is only being called once, and the function does not touch the session in any way (I don't call session variables or anything from within it). Somehow, for some reason, the function returning a value kills the session, but session_write_close() keeps the session alive!
Why on earth does adding session_write_close() before I return a value in the function that I'm calling from the index page actually keep the session alive?
Can you post your script - I've never once needed to use session_write_close (unless you're using frames, you shouldnt need to use it).... I suspect something else is going on.
Do you have session_start() at the top of login.php and index.php?
Also is register_globals on? If so you might be inadvertently overwriting a session variable in your script (by using a variable with the same name as the session key)
posted by missmagenta at 10:17 PM on June 3, 2011
Do you have session_start() at the top of login.php and index.php?
Also is register_globals on? If so you might be inadvertently overwriting a session variable in your script (by using a variable with the same name as the session key)
posted by missmagenta at 10:17 PM on June 3, 2011
Check your error logs. It sounds like the code that uses the return value of your function is throwing an exception, which stops the execution of the script, including skipping the built-in end-of-script cleanup routines which write the session data out to disk. Returning nothing likely means that the offending code is skipped, and manually calling the write function forces the session data to be written out.
posted by Rhomboid at 9:23 AM on June 4, 2011
posted by Rhomboid at 9:23 AM on June 4, 2011
This thread is closed to new comments.
posted by sonic meat machine at 8:26 PM on June 3, 2011 [1 favorite]