Running script at session termination

I am looking at how I can run a script or function when a user session ends so that I can delete temporary files that may be required.

I searched around SO to find a solution to this problem and stumbled upon this one , but I find that I am a bit out of depth and I find the PHP documentation regarding what the GC is confusing (and from what I understand from the documentation - this is not exactly what I need).

I looked at the session_set_save_handler page and came up with the following:

 session_set_save_handler( '', '', '', '', 'deleteDocs'); function deleteDocs() { rmdir('user/' . rawurlencode($_SESSION['data']['user']['details']['email']) . '/temp'); } 

However, this function does not start at all. Honestly, with this, I feel like I don’t know what I'm really doing.

I tried using GC and looked at session.gc_divisor and session.gc_probability , but I still can't edit php.ini.

Can this be achieved. Should I use the cron job - if so, how can I verify that the sessions are finished?

+4
source share
1 answer

Should I use the cron job - if so, how can I verify that the sessions are finished?

Yes. You do this based on the last modification time of the file (in your case, the file is a directory). If it is older than the specified age, delete the directory.

You can use find Wikipedia for this.

Do you find an example script in your PHP folder, see also Removing old session files from the user folder of job sessions? and clear php session files .

+2
source

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


All Articles