Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Save Session when using session_write_close();

Tags:

I've one page where i do a long polling i've to use at the begin of this page this

session_start(); session_write_close(); 

Because :

to prevent concurrent writes only one script may operate on a session at any time

So if i do not and the long polling is running the user will not be able to load another page.

So accessing to my data in session from this polling page is possible but at some point in my script i've to save my session back to the server because i made some change in it.

What's the way to do it?

That will be very nice it'll be a way to do something like

session_write_open(); //do stuff session_write_close(); 

But the session_write_open() doesn't exist!

Thanks

like image 239
Jerome Ansia Avatar asked Apr 06 '12 16:04

Jerome Ansia


People also ask

What does Session_write_close do in PHP?

PHP - session_write_close() Function The session_write_close() function stores the session data (usually stored after the termination of the script) and ends the session.

How can save session data in PHP?

To use session variables, it's necessary to start the session by using the session_start function, this will allow you to store your data in the global variable $_SESSION in a productive way. <? php session_start(); ?>

When session_start () is called PHP will call the open and read session save handlers?

Description ¶When session_start() is called or when a session auto starts, PHP will call the open and read session save handlers. These will either be a built-in save handler provided by default or by PHP extensions (such as SQLite or Memcached); or can be custom handler as defined by session_set_save_handler().

How are sessions stored in PHP?

PHP Session Start By default, session data is stored in the server's /tmp directory in files that are named sess_ followed by a unique alphanumeric string (the session identifier).


2 Answers

The previous solution will create a session ids and cookies... I wouldn't use it as is:

Session is created every time you call session_start(). If you want to avoid multiple cookie, write better code. Multiple session_start() especially for the same names in the same script seems like a really bad idea.

see here : https://bugs.php.net/bug.php?id=38104

I am looking for a solution right now too and I can't find one. I agree with those who say this is a "bug". You should be able to reopen a php session, but as you said session_write_open() does not exist...

I found a workaround in the above thread. It involves sending a header specifying manually the session id's cookie after processing the request. Luckily enough I am working with a home-brewed Front Controller that works so that no sub-controller will ever send data on its own. In a nutshell, it works perfectly in my case. To use this you might just have to use ob_start() and ob_get_clean(). Here's the magic line:

if (SID) header('Set-Cookie: '.SID.'; path=/', true); 

EDIT : see CMCDragonkai's answer below, seems good!?

like image 34
Armel Larcier Avatar answered Sep 22 '22 18:09

Armel Larcier


Before you make some change to the session, call session_start again. Make the changes, and if you still do not want to exit call session_write_close once more. You can do this as many times as you like.

like image 139
Jon Avatar answered Sep 22 '22 18:09

Jon