Why is garbage collection there?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply

Do you have problems using sessions and framesets simultaneously?

Poll ended at Sat Oct 05, 2002 2:36 pm

Yes
2
22%
No
7
78%
 
Total votes: 9

User avatar
Heavy
Forum Contributor
Posts: 478
Joined: Sun Sep 22, 2002 7:36 am
Location: Viksjöfors, Hälsingland, Sweden
Contact:

Why is garbage collection there?

Post 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? :?:
Coco
Forum Contributor
Posts: 339
Joined: Sat Sep 07, 2002 5:28 am
Location: Leeds, UK
Contact:

Post 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
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

Exactly the same... (as Coco) :evil:
Even if I use session_destroy() it doesn't work... :cry:
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post 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.
User avatar
Heavy
Forum Contributor
Posts: 478
Joined: Sun Sep 22, 2002 7:36 am
Location: Viksjöfors, Hälsingland, Sweden
Contact:

Post 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;
}
?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

who finds typos may keep them ;)
User avatar
Heavy
Forum Contributor
Posts: 478
Joined: Sun Sep 22, 2002 7:36 am
Location: Viksjöfors, Hälsingland, Sweden
Contact:

Post by Heavy »

GOD I love this forum!
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

btw: session.gc_maxlifetime = <seconds> not minutes ;)
session.gc_maxlifetime = 1800 --> 30 minutes lifetime
Coco
Forum Contributor
Posts: 339
Joined: Sat Sep 07, 2002 5:28 am
Location: Leeds, UK
Contact:

Post 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...
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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)
Coco
Forum Contributor
Posts: 339
Joined: Sat Sep 07, 2002 5:28 am
Location: Leeds, UK
Contact:

Post 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
User avatar
Heavy
Forum Contributor
Posts: 478
Joined: Sun Sep 22, 2002 7:36 am
Location: Viksjöfors, Hälsingland, Sweden
Contact:

Post 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:
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post 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 ;)
Coco
Forum Contributor
Posts: 339
Joined: Sat Sep 07, 2002 5:28 am
Location: Leeds, UK
Contact:

Post by Coco »

yeah yeah
<pub quiz>
echo 'ooooooo i was just gonna say that'
</pub quiz>
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post 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
Post Reply