Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Thursday, October 20, 2016

delete session files after a time from creation

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

Answer by zneak for delete session files after a time from creation


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.

Answer by psm321 for delete session files after a time from creation


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.

Answer by Jahandideh AR for delete session files after a time from creation


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);      }    }      }  

Answer by tim for delete session files after a time from creation


// Delete old sessions    if (substr(ini_get('session.save_path'), 0, 4) != '/tmp') {      foreach (glob(rtrim(ini_get('session.save_path'), '/') .'/sess_*') as $filename) {        if (filemtime($filename) + ini_get('session.gc_maxlifetime') < time()) {          @unlink($filename);        }      }    }  

Answer by user7050064 for delete session files after a time from creation


// get the session files directory  $dir = session_save_path("session");    //clear cache  clearstatcache();    // Open a directory, and read its contents    if (is_dir($dir)){      // we iterate through entire directory    if ($dh = opendir($dir)){    while (($file = readdir($dh)) !== false){        //get the last acces date of each file      @$time_stamp=fileatime($file);        //check if it is older than... 600, and assign a text flag: with value "to delete" (when old enough) or "---" (when young enough)      $to_delete = ($time_stamp";        //INFO      //depending on wishes, you can modify flow of the script using variables      // particulary useful is $to_delete  -> you can easily covert it to true/false format      //to control the scrip    }      closedir($dh);    }    }  


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.