Jump to content

Lots of Sessions


Pete
 Share

Recommended Posts

Hi folks

So I've got a very active PW site up and running (finally) but I'm not ready to link to it until I finish my snag list :P

I was just looking through the log files, and noticed the site/assets/sessions folder - it has almost 3,000 session files in it already and the site's not been online 24 hours yet!

Are these required for something? They all seem to contain the same content:

Session|a:0:{}

Seems a bit pointless, and I'm not sure what they relate to - any ideas anyone?

Link to comment
Share on other sites

Cheers for that - it only appears to be keeping about 24 hours of them which is how it's suposed to work I see. Fortunately that stops it at 5,600 files ;)

Link to comment
Share on other sites

That's good to know ryan, even though I'm not sure if it's doing any harm with that many files. I was just curious as to why it needed so many empty files. Is it more of a "just in case" thing in case any session vars get set or something?

Link to comment
Share on other sites

This is one of those things that has to do with the inner-workings of PHP's session system. I've not attempted to override PHP's session storage mechanisms with PW (other than where it places the files) so this is probably a question for Rasmus Lerdorf. :) But I believe they are just placeholder files to track an active session, and they won't contain any significant data in them unless you specifically set it to the $session var. In lieu of tracking data, the filename itself is likely of more importance since it relates the session ID from the user's cookie to the session ID on the server. Of course, once we provide the option of DB session storage, we will be taking over more control from PHP in this area... at least when DB session storage is selected.

Link to comment
Share on other sites

  • 11 months later...

If PHP's garbage collector isn't working right, it'll affect DB sessions too. @joe_g, unless your site really handles that many sessions in a day, you may want to look into your PHP garbage collector settings. The relevant PHP settings are session.gc_probability, session.gc_divisor and session.gc_maxlifetime. We set the gc_maxlifetime automatically based on your $config->sessionExpireSeconds setting, but not the other two. However, you can force specific settings for those by adding lines like this to your /site/config.php file:

ini_set("session.gc_probability", 1);
ini_set("session.gc_divisor", 100);
  • Like 3
Link to comment
Share on other sites

  • 1 year later...

hi everyone,

unfortunately i came up to this problem as well. server is Ubuntu 12.04.5 LTS; i had > 140.000 files in the sessions folder :(

i have these two lines in my _init.php file (basic intermediate profile structure)

ini_set('session.gc_probability', 1);
ini_set('session.gc_divisor', 100);

phpinfo in one of my template files says this, so i guess the changes should be working

post-2137-0-35383800-1414579072_thumb.pn

2 days ago i deleted all of the session files, but today i have again > 7000 files, oldest from the day before yesterday, so far over 24 hours old!

any ideas? thanks in advance!

Link to comment
Share on other sites

I would give it a little bit more time. It's not always true that no older files than 'current time - 86400 seconds' should exist.

probability/divisor = 1/100 = 1% : The garbage collector will run once per 100 new sessions, and then clear all files older than 86400.

Although, if you already have 7000 session files, it should be pretty close to exactly 86400 seconds.

  • Like 2
Link to comment
Share on other sites

hmm... thank you sinnut for your fast reply! i'll give it some more days.

just for the record, i have >7500 files right now. lets see how that changes over the next days...

couldn't i also try to increase probability to 99 or 100? to make sure that it works and then change it back to 1?

Link to comment
Share on other sites

hmm... thank you sinnut for your fast reply! i'll give it some more days.

just for the record, i have >7500 files right now. lets see how that changes over the next days...

couldn't i also try to increase probability to 99 or 100? to make sure that it works and then change it back to 1?

Yes you could try setting the probability to '100' for testing purposes. Garbage collection *should* then run on every session start.

If that still does not work i'm not sure what's happening, because your settings seem to be fine. Maybe GC is somehow unable to delete in site/assets/sessions

  • Like 1
Link to comment
Share on other sites

The reason for this is that you are setting the divisor and the probability after the execution of the garbage collector has already been decided (in other words, after the execution of session_start()). With Ubuntu your default value for the probability is 0, meaning it will never execute. You need to place those ini_set directives before the session is started. In ProcessWire one way to do this would be

wire()->addHookBefore("Session::init", function() { 
  ini_set('session.gc_probability', 1);
  ini_set('session.gc_divisor', 100);
}); 

You would need to place this inside an autoloading module though. Another way would be setting those inside your .htaccess with

php_value session.gc_divisor 100
php_value session.gc_probability 1

(depending on your host, this might not work)

Your last resort would be clearing them "manually" by adding this to your _init.php. Well basically you wouldn't even need a hook, you could just execute the code.

wire()->addHookAfter("ProcessPageView::finished", function() {
  $prob   = ini_get('session.gc_probability');
  $div    = ini_get('session.gc_divisor');
  $path   = ini_get('session.save_path'); 
  $expire = ini_get('session.gc_maxlifetime'); 

  if(rand(1,$div) <= $prob))
  {
    foreach(glob($path.DIRECTORY_SEPARATOR."sess_*") as $file)
    {
       if(filemtime($file) <= time() - $expire) unlink($file);
    }
  }
});
  • Like 8
Link to comment
Share on other sites

hey guys, thank you!

it worked for me with the following values (for testing):

index.php

ini_set("session.gc_maxlifetime", 60); // changed $config->sessionExpireSeconds to 60

config.php

ini_set('session.gc_probability', 100); // 100% probability
ini_set('session.gc_divisor', 100);

----> it cleared all session files older than 60 seconds on every pageload.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...