How can a server file record when a web page rendered in Chrome was closed ?
How can a server file record when a web page rendered in Chrome was closed ?
(OP)
How can a server file record when a web page rendered in Chrome was closed ?
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS Contact USThanks. We have received your request and will respond promptly. Come Join Us!Are you a
Computer / IT professional? Join Tek-Tips Forums!
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail. Posting Guidelines |
How can a server file record when a web page rendered in Chrome was closed ?
|
Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.
Here's Why Members Love Tek-Tips Forums:
Register now while it's still free!
Already a member? Close this window and log in.
RE: How can a server file record when a web page rendered in Chrome was closed ?
I see you posted it in the AJAX forum, so you know the answer : send an XMLHttpRequest call to notify the server.
That is the
goodold portable way. But now there is the Beacon API's sendBeacon Method, specially designed for this situation. Less portable for now ( see Can I use... beacon ), but more reliable.Feherke.
feherke.ga
RE: How can a server file record when a web page rendered in Chrome was closed ?
Thanks, but how is sendBeacon used ? I tried the following to store the time the page is unloaded but it didn't
seem to run the alert.php to change alert.txt.
<html>
<head>
<title>alert.htm</title>
</head>
<body>
alert.htm
<script type="text/javascript">
window.addEventListener('unload', logData(), false);
function logData()
{
navigator.sendBeacon("alert.php", nowhhmm() );
}
function nowhhmm() //with leading zero
{
var now = new Date();
var hournow = now.getHours();
var minutenow = now.getMinutes();
var hhmm = hournow*100 + minutenow;
hhmm = ("0" + hhmm).slice(-4);
return hhmm;
}
</script>
</body>
</html>
<?php
//alert.php
$data = $HTTP_RAW_POST_DATA;
$file = 'alert.txt';// Open the file to get existing content
$prev = file_get_contents($file);
$total = $data . $prev . "\n";
file_put_contents($file, $total);
////fclose($file); //not needed ?
exit();
?>
Merry Christmas.
RE: How can a server file record when a web page rendered in Chrome was closed ?
It works for me in FireFox 50.1.0, just the beacon is sent in wrong moment, because you call logData() when setting up the event handler - you should reference it instead :
CODE --> JavaScript ( fragment )
Regarding your PHP code, do you realize you always put the newline at the end of file, not between previous and new data ? ( Also no idea why you need the times logged in reverse order. ) Anyway, first of all, please check whether the web server's user has permission to write in that directory. Check your web server's error log, probably you will find something like this :
Certainly not. $file is a string. You can't close a string.
Feherke.
feherke.ga