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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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)){}).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.