Jump to content

Conditional session garbage collection handling


cranky

Recommended Posts

I've been trying to find a way to have the server collect session garbage more often when there are few users registered, and the other way around. It took me a while, but I've come up with this:

$result = mysql_query("SELECT COUNT(*) FROM users") or ErrorHandlingFunction('foo bar'));

    $numberofusers = mysql_result($result, 0);
    $divisor = 100/$numberofusers;

    ini_set('session.gc_divisor', $divisor);
    ini_set('session.gc_probability', 1);

 

Will this work?

There's no good reason to be doing this.

 

In fact, by reducing the probability that garbage collection will run, as you add more users, when garbage collection does eventually run, it will have to take a greater amount of processing time because there will be more session data files to examine and a greater percentage of those will be old enough to delete and it will take more time to delete them. It is best to just leave the probability at a fixed and reasonable value.

Thank you for your quick reply!

 

There's no good reason to be doing this.

But will it work? I am certainly not getting any errors.

 

Purely hypothetically, if I would do this: $divisor = $numberofusers; What will happen if the number of users goes over a 100? Can divisor be set to a number higher than 100?

 

It is best to just leave the probability at a fixed and reasonable value.

Which would be?

There's no good reason to be doing this.

But will it work? I am certainly not getting any errors.

Hmm, I now see that users are being logged out in the middle of their session, although session.gc_maxlifetime is set to 0 (doesn't this mean until the end of the session, like for cookies?). I was hoping to make sessions infinite (until user navigates away from the site), because I am handling login lifetime a different way (if (time() - $_SESSION['last_activity'] > $set_sessionLifetime)){}).

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.