質問

First, my concrete 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 info. We have a web application with a Frameset (don't get me started... no, there's no money to change this) and we found that in some cases our SESSION variables were not being written to the database! After some arduous testing we found that two frames were being loaded concurrently, and while it almost never happens, the first-called frame finished after the second. The first-called frame was overwriting the session with a prior-made copy (since, at the end of script it writes the session).

Our solution now is to try to call session_write_close() on the first-called frame as soon as we can, but we are concerned about being able to still read the session variables (with 100% certainty).

役に立ちましたか?

解決

Well thinking about it you could implement in a other way.

How to implement it is quiet simple

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

This will be put above in your script.

When the script stops it will be saved to your database, but wait stop, we are not done :P

Then you check if the timestamp in the current record is heigher or lower. if it is lower then update, else forget to update and just do nothing. Should consider microtime(). because people love to press f5.

So new idea, and I think it works a little bit better and saver for production to


To answer the comment:

Look at session handler, you can make your own function how php handles session. So you don't have to refactor you code, only add.

Then in the write part, you say to the database update the record with id x and where the timestamp is higher then the given value.

For the implementation you should add a column to the record timestamp/mincrotime. And in the session handler you save the time it was request. So you can make the comparison. Then you don't need to save the time in the session either :) (not in the $_SESSION, it is saved in the db!)

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top