INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

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 ?

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 ?

RE: How can a server file record when a web page rendered in Chrome was closed ?

Hi

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 good old 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 ?

(OP)
Feherke,
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 ?

Hi

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 )

window.addEventListener('unload', logData, false);
//                          no () here --^ 

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 :

Quote (/var/log/apache2/error.log)


[Sat Dec 24 15:42:55.416260 2016] [:error] [pid 7654] [client 127.0.0.1:55198] PHP Warning: file_get_contents(alert.txt): failed to open stream: No such file or directory in /var/www/html/alert.php on line 5, r
eferer: http://localhost/peterpann.html
[Sat Dec 24 15:42:55.416285 2016] [:error] [pid 7654] [client 127.0.0.1:55198] PHP Warning: file_put_contents(alert.txt): failed to open stream: Permission denied in /var/www/html/alert.php on line 7, referer:
http://localhost/peterpann.html

Quote (peterpann)

////fclose($file); //not needed ? 
Certainly not. $file is a string. You can't close a string.

Feherke.
feherke.ga

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

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.

Join Us             Close