Reading session variables after session_write_close

Firstly, my specific question. Should this work?

<?php session_start(); $_SESSION['test']='TEST_CONTENT'; echo '1: ',$_SESSION['test']; echo '<br>'; session_write_close(); echo '2: ',$_SESSION['test'] ?> 

Now, some background information. We have a web application with Frameset (don't make me start ... no, there is no money to change this) and we found that in some cases our SESSION variables were not written to the database! After some difficult testing, we found that two frames load at the same time, and although this almost never happens, the first so-called frame ends after the second. The first so-called frame rewrote the session with a previously made copy (since at the end of the script it records the session).

Our solution now is to try to call session_write_close () in the first so-called frame as soon as possible, but we are worried that we can still read session variables (with 100% certainty).

+4
source share
1 answer

Thinking about it well, you could implement it differently.

How to implement it quietly, simply

 session_start(); $_SESSION['timestamp'] = time(); 

This will be indicated above in the script.

When the script stops, it will be saved in your database, but stop, we will not finish: P

Then you check if the timestamp in the current record is higher or lower. if it is lower, it is updated, otherwise forget to update and just do nothing. Microtime () should be considered. because people like to press f5.

So, a new idea, and I think it works a little better and saves on production


To reply to a comment:

Look at the session handler , you can make your own function how php processes the session. Thus, you do not need to reorganize the code, add it.

Then, in the part of the record, you will tell the database to update the record with the identifier x and where the timestamp is above the specified value.

To implement, you must add a column to the / mincrotime timestamp. And in the session handler, you save the request time. Therefore, you can make a comparison. Then you do not need to save time in the session either :) (not in $ _SESSION, it is saved in db!)

+1
source

Source: https://habr.com/ru/post/1484825/


All Articles