Home » Php » php – delete session files after a time from creation

php – delete session files after a time from creation

Posted by: admin July 12, 2020 Leave a comment

Questions:

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

How to&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.

Answer:

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:

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:

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

// 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<time()-600) ? 'to delete!' : '---';

    //format acces date to a human readible format
    @$date = date("F d Y H:i:s.",fileatime($file));

    //output stats on the screen
    echo "file:" . $file . "Last access: ".$date.", ".$to_delete."<br>";

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

}