Class clean up (Destructor) in PHP4

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
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Class clean up (Destructor) in PHP4

Post 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!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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)..
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post 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. :?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

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