I’m using database sessions in CakePHP, saving also the user id in the same table as the sessions. Is it possiblee to detect somehow when a user session expires so I can update one field in the database?
I need that in case the user doesn’t log out manually from the website (just closes the browser or not even that) I can remove that session from the table.
Session data cleanup relies on garbage collection (gc). This garbage collection process is only invoked when a PHP script runs, i.e. it’s not cleaning up data right when it could be cleaned up, but some time afterwards when another PHP script is running. Since garbage collection also takes up some resources, it does not run every time, but there’s a certain probability that it’ll be run for each script invocation. The default is that gc runs about once every 100 script invocations.
So you will not see stale data disappear immediately. Rather, it’ll be cleaned out eventually.