views:

313

answers:

3

hello,

I am saving my sessions in another directory from /temp directory. say /session directory.( using session_save_path("session") )

also, there is a code to kill the session after 10 minuets from creation and logout.

but I mentioned that if the user logs in and for example shut down his computer, my log out and session destroy code doses not run, so the session file will remain in the session directory.

I wanted to know is there is a way to delete session files in /session after a time from creation?

I used this code for it

  if ($handle = opendir('sessions')) {

    while (false !== ($file = readdir($handle))) {
        if (filectime($file)< (time()-600)) {  // 600 = 10*60
        unlink($file);
        }
    }
  }

but, not working, I think it couldn't get the creation time by filectime($file)

thanks

+2  A: 

You shouldn't need that. PHP itself implements a garbage collection mechanism to delete defunct session files. It's going to be much more efficient than anything else you could write yourself using PHP.

See the session.gc_* configuration options of PHP for more infos.

zneak
It seems that it works only with sessions in the /tmp directory I have 4 session files in there( my own session directory), from 4 days ago still alive !!
safaali
I'm quite sure it works with your current session path. Remember there's only a small chance that the GC mechanism actually starts each time you run your script to avoid too much overhead. By default, it's 1% chance each time you access a script using sessions.
zneak
so, how can I change the "session.gc_probability" to 100 ( it's default is 1 )
safaali
@safaali - That's not the right solution at all. Causing the garbage collector to run on **every** session fetch is adding a lot of overhead. Why not simply store an `expiryTime` as part of the session information, and check it when you start the session? If it's too old, you can explicitly destroy it with `session_destroy()` and start fresh with a new session.
zombat
@zombat, i am doing that, I set a creation time for session after login, and after 300 seconds it will destroyed by logout.php, also I have a code that counts the number of session files in session directory and tells the number of online users, the problem arise when a user logs in and do not log out and turns off his computer (so the session file is not destroyed but the user is not online), and what we get is online users +1 more !!
safaali
A: 

I've done this before with a cron job that went in and deleted session files older than X (for some reason PHP's automatic cleanup wasn't doing the job). Unfortunately that's probably not an option you'd have if this is on a managed host that doesn't give you the ability to set up cron jobs.

psm321
A: 

thanks but I think I could solve it by myself

the solution was simple

  if ($handle = opendir('sessions')) {

  foreach (glob("sessions/sess_*") as $filename) {
    if (filemtime($filename) + 400 < time()) {
      @unlink($filename);
    }
  }

  }
safaali