onleave?

T

Thomas Henz

Hi!

I'm searching for something that lets me write into a mysql db (or just
do sth) when a user leaves a site. that means if the user closes the
browser window. How can I do such a thing? I heard something about a
<body onleave=""> but it doesn't seem to be implemented in browsers or
at least some of them.

Thanks,

Tom
 
D

David Dorward

Thomas said:
I'm searching for something that lets me write into a mysql db (or just
do sth) when a user leaves a site. that means if the user closes the
browser window. How can I do such a thing? I heard something about a
<body onleave=""> but it doesn't seem to be implemented in browsers or
at least some of them.

You're probably thinking of onunload - but its useless for what you are
planning.

There is no effective way of doing anything on the server in responce to the
user leaving a page.
 
J

Joel Shepherd

Thomas said:
Hi!
Hi!

I'm searching for something that lets me write into a mysql db (or
just do sth) when a user leaves a site. that means if the user
closes the browser window. How can I do such a thing?

There's no reliable way to do this. HTTP is stateless, so once your
server has filled the user's latest request, you have no way of
knowing if they're viewing it, have closed their browser, have typed
in a new URL and scooted off elsewhere, etc.

About the best you can do is have a session on the server side for
each user that expires after a certain period of inactivity: e.g.,
after 10 minutes with no further requests in that session. When the
session expires, you assume the user has "left the site" and do
something accordingly. Cookies and/or hacked URLs are the usual means
for tracking sessions.
 
T

Thomas Henz

Joel said:
About the best you can do is have a session on the server side for each
user that expires after a certain period of inactivity: e.g., after 10
minutes with no further requests in that session. When the session
expires, you assume the user has "left the site" and do something
accordingly. Cookies and/or hacked URLs are the usual means for tracking
sessions.

How can I do that 10 minutes thing without access to mysql files on the
providers side? or is that a php session thing (i smell OT)?

tom
 
J

Joel Shepherd

Thomas said:
How can I do that 10 minutes thing without access to mysql files on
the providers side? or is that a php session thing (i smell OT)?

It's probably a PHP thing. Hopefully PHP has some way of notifying you
(in code) when a session has expired. Catch that notification, write
something to your database, and move on.

I'm being vague because I haven't used PHP specifically, and in any
event sessions can be and are implemented in many different ways.
 
D

Darren Benfer

Hi!

I'm searching for something that lets me write into a mysql db (or just
do sth) when a user leaves a site. that means if the user closes the
browser window. How can I do such a thing? I heard something about a
<body onleave=""> but it doesn't seem to be implemented in browsers or
at least some of them.

Thanks,

Tom

I don't think there is a way to this within php. My thought is that you
could start a session and log the session-id into mysql for every page
access, then have a cron job that checks for sessions within mysql that
have been inactive for a short while. If it finds one, kill the session and
remove the mysql row. But it seems like an awful lot of work.

The other thing (*shudder*) is to use javascript's onblur or onclose and
open a pop-up to a short php script that does the save, but I wouldn't
recommend it. Pop-ups for such a purpose would be a bit rude! :)

GL...
 
T

Thomas Henz

Darren said:
The other thing (*shudder*) is to use javascript's onblur or onclose and
open a pop-up to a short php script that does the save, but I wouldn't
recommend it. Pop-ups for such a purpose would be a bit rude! :)

I thought about that for some minutes and came to the same conclusion of
being rude to the user. But since I don't have access to a root server
or sth where I could install automatic scripts...well...why not be rude? :)
Couldn't I open a new window and close it immediately after executing
the sql command? Something like "when function finished, window.close()"? :/

Tom
 
C

Chris Morris

Thomas Henz said:
I thought about that for some minutes and came to the same conclusion
of being rude to the user. But since I don't have access to a root
server or sth where I could install automatic scripts...well...why not
be rude? :)

You could just assume that they've left when the session isn't being
used anymore.
Couldn't I open a new window and close it immediately after executing
the sql command? Something like "when function finished,
window.close()"? :/

Depending on the speed of the user's computer, this might get in the
way anyway.

Also, pop-up blockers are becoming more commonplace - built in to most
modern browsers, 3rd party ones available for older browsers such as IE.
 
T

Thomas Henz

Andrew said:
sth=something :/

What I wanted to say is that I don't have access to a server shell or
cronjobs etc. that could do things automatically for me.
 
D

Darren Benfer

I thought about that for some minutes and came to the same conclusion
of being rude to the user. But since I don't have access to a root
server or sth where I could install automatic scripts...well...why not
be rude? :) Couldn't I open a new window and close it immediately
after executing the sql command? Something like "when function
finished, window.close()"? :/

Tom

Yep!

(And I'm crossposting this to alt.php in case you need any other php
help)

Here's the pseudo-code (untested):

NORMAL PAGE CODE:
<html>
<body onClose="window.open('database_operation.php');">
<!-- page stuff -->
</body>
</html>

DATABSE_OPERATION.PHP CODE:
<?php
// do database stuff
if (database stuff done) {
echo "<html><body onLoad=\"window.close();\">";
echo "</body></html>";
exit;
} else {
die("Database error! Closing script.");
}
?>

You might need something other that onClose (onBlur?) Real raw, but hope
it helps...
 
D

Darren Benfer

sth=something :/

What I wanted to say is that I don't have access to a server shell or
cronjobs etc. that could do things automatically for me.

In that case this might be the best way. (so long as the person has
javascript on...)

GL!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top