Destroying Objects

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I have a need to notice when a user leaves a site so I can implement
destructors for objects. I know that PHP doesn't supply an object
destructor. I can code that myself. What I don't know how to do is know when
a session is no longer needed, for example, when a user leaves the site or
closes the browser. I thought that register_shutdown_function() would help,
but it shuts down at the end of the session regardless of whether I plan to
start it again on the next page.

I'd appreciate any ideas.

Re: Destroying Objects

There is no way to reliably detect if a user has left the site/closed their
browser/computer struck by lightning.  You could have pages automatically
refresh periodically (or use a javascript call) and check for sessions older
than that period, but it wouldn't be infallible.

What is the end result you are trying to accomplish?

- Kevin

Quoted text here. Click to load it

Re: Destroying Objects

Quoted text here. Click to load it

As Kevin pointed out there is no foolproof way to know when a user has left
the sight.

A typical method (used by PHP's builtin session handler, too) is to assume
that a certain period of inactivity means they left the site.

If you built a custom session handler, on cleanup of dead sessions you could
have it execute your "visitor left the site" code for all sessions that
expired.  The disadvantage, of course, is that it will not be run for
however long you set the session expiry to be.  For instance, is sessions
expire after 15 minutes of inactivity, then the "visitor left site" code
will not get run until they've already been gone 15 minutes.

Kevin also suggested some JS solutions.  You could try those.  As usual, the
caveat is that JS can easily be disabled, might be broken in the visitor's
browser, etc.


Site Timeline