Jahandideh AR Jahandideh AR - 1 year ago 91
PHP Question

delete session files after a time from creation

I am saving my sessions in another directory from /temp directory.

directory.( using

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
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

but, not working, I think it couldn't get the creation time by


Answer Source

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()) {

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download