THe following code works locally on xampp but not on 5 different servers on-line. How to make the code work on-line also ? The plan is to show how long successive visitors have spent on the ajaxtest.htm page. The files involved are ajaxtest.htm, ajaxtestpost.php, ajaxtestvisits.txt, ajaxtestvisits.php and are on-line at
They are also duplicated at
with the same results
<html>
<head>
<title>ajaxtest.htm</title>
</head>
<body onbeforeunload="beforeleavepage()">
ajaxtest.htm to store durations this page visited in ajaxtestvisits.txt
<script type="text/javascript">
<!--Hide
function getstarttime() //when page loads
{
var now = new Date();
return now.getTime();
}
function beforeleavepage()
{
var now = new Date();
var milliseconds = now.getTime() - starttime;
var duration = (milliseconds/1000).toFixed(1); //7.3
var httppost = new XMLHttpRequest();
httppost.open("POST", "ajaxtestpost.php", true);
var postdata = "duration="+duration;
//Send the proper header information along with the request
httppost.setRequestHeader("Content-type", "application/x- httppost.setRequestHeader("Content-length", postdata.length);
httppost.send(postdata);
}
var starttime = getstarttime(); //milliseconds
//stop hiding-->
</script>
</body>
</html>
........................
<?php
//ajaxtestpost.php
$duration = $_POST["duration"];
$file = 'ajaxtestvisits.txt';// Open the file to get existing content
$prev = file_get_contents($file);// Append new data to the file
$current .= $duration . " ";
$total = $current . $prev . "\n";
file_put_contents($file, $total);
fclose($file);
exit();
?>
......................
<?php
//ajaxtestvisits.php
$file = 'ajaxtestvisits.txt';
$prev = file_get_contents($file) . $message;
echo $prev;
?>
....................
6.8 5.0 ajaxtestvisits.txt
They are also duplicated at
with the same results
<html>
<head>
<title>ajaxtest.htm</title>
</head>
<body onbeforeunload="beforeleavepage()">
ajaxtest.htm to store durations this page visited in ajaxtestvisits.txt
<script type="text/javascript">
<!--Hide
function getstarttime() //when page loads
{
var now = new Date();
return now.getTime();
}
function beforeleavepage()
{
var now = new Date();
var milliseconds = now.getTime() - starttime;
var duration = (milliseconds/1000).toFixed(1); //7.3
var httppost = new XMLHttpRequest();
httppost.open("POST", "ajaxtestpost.php", true);
var postdata = "duration="+duration;
//Send the proper header information along with the request
httppost.setRequestHeader("Content-type", "application/x- httppost.setRequestHeader("Content-length", postdata.length);
httppost.send(postdata);
}
var starttime = getstarttime(); //milliseconds
//stop hiding-->
</script>
</body>
</html>
........................
<?php
//ajaxtestpost.php
$duration = $_POST["duration"];
$file = 'ajaxtestvisits.txt';// Open the file to get existing content
$prev = file_get_contents($file);// Append new data to the file
$current .= $duration . " ";
$total = $current . $prev . "\n";
file_put_contents($file, $total);
fclose($file);
exit();
?>
......................
<?php
//ajaxtestvisits.php
$file = 'ajaxtestvisits.txt';
$prev = file_get_contents($file) . $message;
echo $prev;
?>
....................
6.8 5.0 ajaxtestvisits.txt