Forum Moderators: coopster
Most operations that need to know when the user leaves, simply use some form of timeout, say 60minutes. If for 60 minutes the same IP doesn't not request any other pages, asume he's left.
If it's a different kind of action, the only way would be to rout the user through another page first. This would very much annoy them and is not recommended.
The last possibility I can think of is to try to load an image for example that then executes the PHP function on the server. you could try to load it into an invisible item, but again I'm not sure if this will fail due to the browser unloading the page.
The last possibility is a pop up with all it's accosiated misgivings and of course issues with popup blocking software.
All in all I'd try to redesign the system not to need that last PHP call at all.
SN
(EDITED)I posted right after "killroy" so I didn't see his message... I recomend this :
Most operations that need to know when the user leaves, simply use some form of timeout, say 60minutes. If for 60 minutes the same IP doesn't not request any other pages, asume he's left.
(EDITED END)
You cannot call a PHP function from javascript. PHP is service-side and Javascript is client-side.
You can do the following tho.
<body onunload="window.open('PAGE.php','goodbye','width=20,height=20');"> and the page that pops up runs the PHP function.
on the page that pops up you can add the following so once the php function is finished it closes the pop up window.
<body onload="window.close();"> People are ganna hate me for helping you with this lol (people hate when a site is spying on them)
[edited by: patrixs at 4:18 pm (utc) on July 24, 2003]
[webmasterworld.com...]
Use onunload/pop-up window, log time to server
un-php.htm
<html><head>
<title>access php</title>
</head>
<body onunload="window.open('un-php.php','o','width=1,height=1');">
This is a page.
</body></html>
////////////////
Log time
un-php.php
<?
print "<body onload=\"window.close();\">";
$hour_min_sec = getdate();
$hour = $hour_min_sec['hours'];
$min = $hour_min_sec['minutes'];
$sec = $hour_min_sec['seconds'];
$handle = fopen ("/home/georgegg/log-php/js-php.txt", "a+");
fwrite($handle, "$hour:$min:$sec\n");
fclose($handle);
?>
//////////
//////////
Access PHP getdate() function with JS
js-php.htm
<html><head>
<title>access php with js src attribute</title>
</head>
<BODY">
Set current_hour_min_sec_var to "Unknown"<br>
Show variables values<br>
<script>
// same variables as returned by .php file
var current_hour_min_sec_var = "Unknown";
document.write ("current_hour_min_sec_var = "+current_hour_min_sec_var+"<br>");
</script>
<br>
Access php file on server:<br>
<script>
document.write("<script src=\"js.php\"></"+"script>");
</script>
Should have returned hour:minute:second variable from php on server<br>
<script>
document.write ("current_hour_min_sec_var = "+current_hour_min_sec_var+"<br>");
</script>
<br>
</center>
</body></html>
////////
js.php
<?
header("Content-type: application/x-javascript\n\n");
$hour_min_sec = getdate();
$hour = $hour_min_sec['hours'];
$min = $hour_min_sec['minutes'];
$sec = $hour_min_sec['seconds'];
print "current_hour_min_sec_var = '$hour:$min:$sec';\n";
// could also log to server
?>
BTW I'm 'not' a PHP or JS person.
GGG