Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

delete session files after a time from creation

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

like image 335
Alireza Avatar asked Jan 24 '10 04:01

Alireza


1 Answers

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.

like image 117
zneak Avatar answered Sep 20 '22 22:09

zneak