Page 1 of 1

Class clean up (Destructor) in PHP4

Posted: Mon Aug 23, 2004 7:11 pm
by evilmonkey
Hello. I know that many people are already blessed with PHP5. Unfortunatly, not bieng one of them, I still have to do my code in PHP4, and came accross needing to use a destructor.

Basically, I have a user class that's stored in the $_SESSION[] autglobal. When the user logs out or leaves the site or closes the window, I need PHP to do a query to SQL database. I know of register_shutdown_function(), but I don't really understand how to use it. Can someone please give me a pointer? Thanks!

Posted: Mon Aug 23, 2004 7:22 pm
by feyd
I believe the shutdown function is called when the page processing ends.. You may want to look into setting your session handlers garbage collector probability and divisor so it'll run more often, so you can check the last time the session was used and kill the data after 3 hrs or something..

Posted: Mon Aug 23, 2004 7:26 pm
by evilmonkey
Hmmm...how would I do that?

Basically, the column I need to update in the database is online/offline, I'm trying to create a tracker for who's online, who isn't, I need to do it though the database due to the disign of my code.

Posted: Mon Aug 23, 2004 7:47 pm
by feyd
right, and that's how probably many of us have done it..

Basically, here's how I have my who's online setup.. (similar to phpbb)

when someone, anyone, accesses a page, the session table is looked at.. any sessions older than (enter your max life limit here) is deleted.. then you count how many were updated in the last (enter your "online" life span here)..

Posted: Mon Aug 23, 2004 9:59 pm
by evilmonkey
How do I measure the life of a session? And what if a user is on longer than some time? Not exctly accurate data because if someone is on for 5 minutes, and the session lasts 3 hours (like you suggested), it is inacurate. At the same time, if someone spends 12 hours on my site (not likely, but hey), they'd end up getting shown as offline after 3 hours. :?

Posted: Mon Aug 23, 2004 10:08 pm
by feyd
store a start time, and last update/access time.. each page updates the last time field.. you can tell how long they've been online, and how recently they've looked at a page.

the who's online should only look for people active (last access time) is less than 5 or 10 minutes old.. something like that..

so most of your checks would run off the last access time field..

Posted: Tue Aug 24, 2004 1:34 am
by John Cartwright
heres what i do


page load:

insert into `whos_online` -> $time = current time + 300
delete from `whos_online` -> where time = current time


select distinct users from `whos_online`


thats what feyd said in a nutshell.

Posted: Tue Aug 24, 2004 8:11 am
by evilmonkey
Hmm, all this seems to boil down to one thing: more mysql queries, something I've been trying to minimize. Very well. Phenom, why do you delete from whos_online where time=current time? Shouldn't it be current_time-db_time=5 minutes, then delete?