You cannot use the session_destroy handler, because it is only called if you explicitly call session_destroy, which will not happen if the user simply closes his browser and ends the session. Since there is no way in this scenario to capture the end of an event session, you will have to do some periodic checking to see if the session has passed within a certain time period. The easiest way to do this is to create a field in the session that contains the last access time, and then create a script that periodically (either via cron or runs some other functions in your PHP scripts) checks this field for all active sessions and for those If you have a timestamp older than xx seconds, run your update request.
Please note that for this, using standard PHP sessions that are stored in the file system, you will actually have to open the files and analyze them directly. An alternative is to save the sessions in the database, but this is probably not what you want to do, given the fact that you are trying to reduce the load on db.
Another opportunity that is not developing so intensively:
- Add timestamp with last session access
- Check this tag every time you update a session
- If the timestamp is older than xx seconds, run the update request on db and set the timestamp at the current time.
Thus, you update the database every so often, and not every page load. The disadvantage of this approach is that if the last action on the session exactly matches the time interval that the update function calls, you cannot store the latest session information in db.
Theox source share