Ajax 和/或 PHP 错误

发布于 2024-12-27 00:07:19 字数 3628 浏览 0 评论 0原文

我使用 AJAX 和 PHP 将访问者在页面上花费的时间传达给服务器,其中 PHP 脚本将花费的时间以及其他详细信息写入文本文件。 AJAX 脚本无法调用 php 脚本,因此未记录访客详细信息

<script type="text/javascript">
    var startTime = new Date();        //Start the clock!
    window.onbeforeunload = function()        //When the user leaves the page(closes the window/tab, clicks a link)...
    {
        var endTime = new Date();        //Get the current time.
        var timeSpent = (endTime - startTime);        //Find out how long it's been.
        var xmlhttp;        //Make a variable for a new ajax request.
        if (window.XMLHttpRequest)        //If it's a decent browser...
        {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();        //Open a new ajax request.
        }
        else        //If it's a bad browser...
        {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");        //Open a different type of ajax call.
        }

        xmlhttp.open("GET","ajaxphp.php?time="+timeSpent,false);        //The false at the end tells ajax to use a synchronous call which wont be severed by the user leaving.
        xmlhttp.send();        //Send the request and don't wait for a response.
    }
</script>

这是 AJAX 脚本

PHP 脚本也如下:

<?php
$time=$_GET["time"];

$countfile = "counter.txt";

// location of site statistics.
$statsfile = "stats.txt";

// checks whether the file exist, if not then server will create it.
if (file_exists($countfile)) {

// open the counter hit file.
$fp = fopen($countfile, "r"); 

// reads the counter hit file and gets the size of the file.
$output = fread($fp, filesize($countfile));

// close the counter hit file.
fclose($fp); 

// get the integer value of the variable.
$count = intval($output);
}

// if file is doesn't exist, the server will create the counter hit file and gives a value of zero.
else { 
$count = 0;
}

// showcount function starts here.
function ShowCount() { 

// declares the global variables.
global $ShowCount, $countfile, $statsfile, $count,$time;

// get the current month.
$month = date('m');

// get the current day.
$day = date('d');

// get the current year.
$year = date('Y');

// get the current hour.
$hour = date('G');

// get the current minute.
$minute = date('i');

// get the current second.
$second = date('s');

// this is the date used in the stats file
$date = "$month/$day/$year $hour:$minute:$second";

// this is the remote IP address of the user.
$remoteip = getenv("REMOTE_ADDR");

// some of the browser details of the user.
$otherinfo = getenv("HTTP_USER_AGENT");

// retrieve the last URL where the user visited before visiting the current file.
$ref = getenv("HTTP_REFERER");

// open the statistics file. 
$fp = fopen($statsfile, "a");

// put the given data into the statistics file.
fputs($fp, "Remote Address: $remoteip | ");
fputs($fp, "Information: $otherinfo | ");
fputs($fp, "Date: $date | ");
fputs($fp, "Referer: $ref\n");
fputs($fp, "Time Spent: $time | ")

// close the statistics file.
fclose($fp);

// adds 1 count to the counter hit file.
$count++;

// open the counter hit file.
$fp = fopen($countfile, "w");

// write at the counter hit file.
// if the value is 34, it will be changed to 35.
fwrite($fp, $count);

// close the counter hit file.
fclose($fp);

// showcount variable is equal to count variable.
$ShowCount = $count;

// return the value of the count variable into showcount variable.
return $ShowCount;
}

// display the value in the counter hits file.
echo showcount(), " visits";

?>

I am using AJAX along with PHP to communicate the time a visitor spends on a page to the server where the PHP scripts writes the time spent along with other details to a text file. The AJAX script is unable to call the php script and hence the visitor details are not being logged

<script type="text/javascript">
    var startTime = new Date();        //Start the clock!
    window.onbeforeunload = function()        //When the user leaves the page(closes the window/tab, clicks a link)...
    {
        var endTime = new Date();        //Get the current time.
        var timeSpent = (endTime - startTime);        //Find out how long it's been.
        var xmlhttp;        //Make a variable for a new ajax request.
        if (window.XMLHttpRequest)        //If it's a decent browser...
        {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();        //Open a new ajax request.
        }
        else        //If it's a bad browser...
        {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");        //Open a different type of ajax call.
        }

        xmlhttp.open("GET","ajaxphp.php?time="+timeSpent,false);        //The false at the end tells ajax to use a synchronous call which wont be severed by the user leaving.
        xmlhttp.send();        //Send the request and don't wait for a response.
    }
</script>

This is the AJAX script

The PHP script is given below too :

<?php
$time=$_GET["time"];

$countfile = "counter.txt";

// location of site statistics.
$statsfile = "stats.txt";

// checks whether the file exist, if not then server will create it.
if (file_exists($countfile)) {

// open the counter hit file.
$fp = fopen($countfile, "r"); 

// reads the counter hit file and gets the size of the file.
$output = fread($fp, filesize($countfile));

// close the counter hit file.
fclose($fp); 

// get the integer value of the variable.
$count = intval($output);
}

// if file is doesn't exist, the server will create the counter hit file and gives a value of zero.
else { 
$count = 0;
}

// showcount function starts here.
function ShowCount() { 

// declares the global variables.
global $ShowCount, $countfile, $statsfile, $count,$time;

// get the current month.
$month = date('m');

// get the current day.
$day = date('d');

// get the current year.
$year = date('Y');

// get the current hour.
$hour = date('G');

// get the current minute.
$minute = date('i');

// get the current second.
$second = date('s');

// this is the date used in the stats file
$date = "$month/$day/$year $hour:$minute:$second";

// this is the remote IP address of the user.
$remoteip = getenv("REMOTE_ADDR");

// some of the browser details of the user.
$otherinfo = getenv("HTTP_USER_AGENT");

// retrieve the last URL where the user visited before visiting the current file.
$ref = getenv("HTTP_REFERER");

// open the statistics file. 
$fp = fopen($statsfile, "a");

// put the given data into the statistics file.
fputs($fp, "Remote Address: $remoteip | ");
fputs($fp, "Information: $otherinfo | ");
fputs($fp, "Date: $date | ");
fputs($fp, "Referer: $ref\n");
fputs($fp, "Time Spent: $time | ")

// close the statistics file.
fclose($fp);

// adds 1 count to the counter hit file.
$count++;

// open the counter hit file.
$fp = fopen($countfile, "w");

// write at the counter hit file.
// if the value is 34, it will be changed to 35.
fwrite($fp, $count);

// close the counter hit file.
fclose($fp);

// showcount variable is equal to count variable.
$ShowCount = $count;

// return the value of the count variable into showcount variable.
return $ShowCount;
}

// display the value in the counter hits file.
echo showcount(), " visits";

?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赢得她心 2025-01-03 00:07:19

尝试以下操作:

  • 直接使用 Web 浏览器访问 url ajaxphp.php?time=123456 并记下发生的情况
  • 在激活 ajax 调用的按钮上使用 onclick 处理程序并记下发生的情况
  • 使用 jquery 代替 ajax 并记下发生的

情况我确信其中之一将为您提供更多线索,了解当前设置失败的原因。

Try the following:

  • Access the url ajaxphp.php?time=123456 directly with your web browser and note what happens
  • Use an onclick handler on a button that activates the ajax call and note what happens
  • Use jquery instead for the ajax and note what happens

I'm sure one of these will give you more clues to why your current setup fails.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文