Page 1 of 1

Why is garbage collection there?

Posted: Wed Sep 25, 2002 2:36 pm
by Heavy
Alright, This really makes me wonder if I should recommend PHP to others.

I Posted a topic recently (A few days ago) about the fact that my SESSION just ended even though I was reloading my "tracking" page.
Now I have tracked the cause of my problem down.:D Big thanks to hob_goblin.

I had these settings for session garbage collection:

Code: Select all

session.gc_probability = 1
session.gc_maxlifetime =
That caused my session to suddenly die without any obvoius reason.

I thought:
AHA! If I modify the session data at every reload, it might not destroy my session just like that. Here is that new tracking code:

Code: Select all

<?php

   require_once $_SERVERї'DOCUMENT_ROOT']."/Includes/Globals.php";
   if (!isset($_SESSIONї'GARBAGE_PREVENTER'])||$_SESSIONї'GARBAGE_PREVENTER']>10)$_SESSIONї'GARBAGE_PREVENTER']=0;
   $_SESSIONї'GARBAGE_PREVENTER']++;
  // the session var above is modified at every reload.

   $Action = "'CheckThings.php?time=".time()."'";
  ?>
  <HTML>
   <HEAD>
   </HEAD>
   <BODY OnLoad ="<? echo $UpdateMenuStr ?>" BGCOLOR="#bbbbbb">
   <SCRIPT LANGUAGE=JAVASCRIPT>
    function Reload(){
    document.location=<?=$Action?>;
    }
    window.setTimeout("Reload()",<? echo intval($_SESSIONї'POLL_INTERVAL'])?>);
   </SCRIPT>
   </BODY></HTML>
?>
I also set those php.ini variables to:

Code: Select all

session.gc_probability = 100
session.gc_maxlifetime = 10
EVEN THOUGH I changed the session data at every reload with $_SESSION['POLL_INTERVAL'] set to 700 msec, PHP just discards my business critical data after 10 seconds.
I understand that PHP needs to remove some old data from the disk space. But why is it removing data that is less than a second old?:evil:

Very strange.

Is there a better solution to this issue that just setting the time limit to 36 000 seconds?:?:

I dont want the site visitors to raise their eyebrows because they have suddenly been logged out without reason.

Does my partition grow full if I set session.gc_probability = 0? :?:

Posted: Wed Sep 25, 2002 2:56 pm
by Coco
i got the opposite
my session never ends 8O

and i dont have access to me php.ini :cry:
and my host doesnt care
but i aint paying for them to host me so i dont care (much)

*runs off to try this script

Posted: Wed Sep 25, 2002 3:21 pm
by Takuma
Exactly the same... (as Coco) :evil:
Even if I use session_destroy() it doesn't work... :cry:

Posted: Wed Sep 25, 2002 4:53 pm
by hob_goblin
try something like this:

Code: Select all

foreach($_SESSION as $key -> value){
$_SESSIONї$key] = $value;
}
that will just update EVERY value in the session array, they will not get changed.

You might try running a cron job to delete the contents of the session folder, then you wouldn't have to worry about GC.

My sessions always end when i close my browser, but they are probably still on the system, cause the cookie is the only thing really getting deleted.

Also, my host has it set to 1.

it's good to know that i helped, and if you have any more problems or my suggestions don't work, just come back and ill try some more.

Posted: Wed Sep 25, 2002 5:47 pm
by Heavy
It hurts to regret that your code:

Code: Select all

<?php
foreach($_SESSION as $key -> value){
$_SESSIONї$key] = $value;
}
?>
... is not valid...

I guess that you ment:

Code: Select all

<?php
foreach($_SESSION as $key => $value){
$_SESSIONї$key] = $value;
}
?>

Posted: Wed Sep 25, 2002 6:12 pm
by volka
who finds typos may keep them ;)

Posted: Wed Sep 25, 2002 6:24 pm
by Heavy
GOD I love this forum!

Posted: Wed Sep 25, 2002 6:36 pm
by volka
btw: session.gc_maxlifetime = <seconds> not minutes ;)
session.gc_maxlifetime = 1800 --> 30 minutes lifetime

Posted: Thu Sep 26, 2002 4:03 am
by Coco
[edit due to stupidity]

so how do i put in a maxlife time without access to php.ini?
i checked my cookies, and i have a cookie from my site that has a sid in it but the only way i know of that will get rid of that is to manually remove it...
i cant edit php.ini, even tho i know tht having a sess id in a cookie isnt really a good idea...

Posted: Thu Sep 26, 2002 7:43 am
by volka
you may try it with something like
ini_set("session.gc_maxlifetime", 600); // 10 minutes
ini_set("session.gc_probability", 100); // 100% probability to remove the data
you have to do it before your first session-operation (session_start, session_register, $_SESSION['var'] = , ...)
The (client-side) cookie for the session should have a lifetime of 0 by default (meaning it is deleted when the browser closes and in the best case stored in memory only, not written to disk).
The php.ini parameter for this is session.cookie_lifetime
you may set it with ini_set as well (but of course also before any session-operation)

Posted: Thu Sep 26, 2002 9:14 am
by Coco
aha! it works!

cheers volka, but a not to anyone else doing this too... you gotta make sure you manually delete the OLD cookie since it has an 'infinate' lifetime (well mine did)

thanks muchly

Posted: Thu Sep 26, 2002 1:24 pm
by Heavy
I'm fully satisfied.
I set 'em this way:

Code: Select all

session.gc_probability = 1
session.gc_maxlifetime = 36000
I left it running while I was sleeping for seven hours.

When I woke up, I hit the mouse to disturb the screensaver.
Great excitement.
MMM... one, two, three... I can almost see it. YES! It still works!

The session didn't die even though I left the computer over the night.

Funny this thread happened to gather others with session related problems opposite of mine. :wink: :lol: :lol:

Posted: Thu Sep 26, 2002 4:14 pm
by hob_goblin
Heavy wrote: I guess that you ment:

Code: Select all

&lt;?php
foreach($_SESSION as $key =&gt; $value){
$_SESSION&#1111;$key] = $value;
}
?&gt;
yes yes yes i know, but when i went to edit it, the server crapped out. i was fully aware ;)

Posted: Thu Sep 26, 2002 6:12 pm
by Coco
yeah yeah
<pub quiz>
echo 'ooooooo i was just gonna say that'
</pub quiz>

Posted: Fri Sep 27, 2002 9:46 am
by BDKR
Is there a better solution to this issue that just setting the time limit to 36 000 seconds?
Yes, don't use the built in sessions for PHP. Store them in a database. It would scale better, preform better, and as the size of you web business grows (hopefully) would be more fault tolerant, not to mention more secure.
Alright, This really makes me wonder if I should recommend PHP to others.
For the reasons I said above, I don't believe that sessions alone would be a good reason to not recomment PHP to others. Sure, sessions may be a little tossed up, but otherwise, PHP is just plain great. (Did I just sound like Tony Tiger?)

Cheers,
BDKR