4

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

I have searched around SO to find a solution to this problem and came across this but I find I am a little out of my depth and I find the PHP documentation regarding GC to be confusing (and from what I understand from the documentation - it is not really what I need).

I looked at the page on session_set_save_handler and came up with this:

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

However, this function does not run at all. Frankly, with this, I feel like I do not know what I am really doing.

I tried using GC and looked at session.gc_divisor and session.gc_probability but I cannot edit php.ini anyway.

Can this be achieved. Should I be using a cron job - if so, how can I check that sessions are finished?

Community
  • 1
  • 1
Joshua Bambrick
  • 2,669
  • 5
  • 27
  • 35
  • `session_set_save_handler` is for replacing PHP's normal session handling with your own custom solution. It's not for chaining functionality. – webbiedave Mar 05 '12 at 18:45

1 Answers1

2

Should I be using a cron job - if so, how can I check that sessions are finished?

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

You can use the findWikipedia command for that.

You find an example script in your PHP folder, see as well Deleting old session files from custom set session folder? and cleanup php session files.

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836