Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/456.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用AJAX将客户端运行时间记录到服务器日志文件_Php_Javascript_Ajax_Logging - Fatal编程技术网

Php 使用AJAX将客户端运行时间记录到服务器日志文件

Php 使用AJAX将客户端运行时间记录到服务器日志文件,php,javascript,ajax,logging,Php,Javascript,Ajax,Logging,因此,我必须将客户端脚本运行时记录到服务器端日志文件中。我测量函数所花费的时间,然后通过ajax向服务器报告。这个代码看起来正确吗?我现在无法访问生产服务器,XAMPP出现故障,在我确定它可以正常工作之前,我不想向我的老板展示这一点。这是我第一次使用AJAX,第二次使用JS 在my index.php中: function search(x,acc) { var startTime = new Date().getTime(); //do wo

因此,我必须将客户端脚本运行时记录到服务器端日志文件中。我测量函数所花费的时间,然后通过ajax向服务器报告。这个代码看起来正确吗?我现在无法访问生产服务器,XAMPP出现故障,在我确定它可以正常工作之前,我不想向我的老板展示这一点。这是我第一次使用AJAX,第二次使用JS

在my index.php中:

function search(x,acc)
    {
           var startTime = new Date().getTime();
           //do work
           //
           var endTime = new Date().getTime() - startTime;
           var outputMessage = "The process took: " + max/1000 + "seconds";
           console.log(outputMessage);
           $.ajax({
           type: "POST",
           url: "ajaxCallback.php",
           data: {outputMessage},
           success: function()
                    {
                        console.log("Client Side : Ajax post     submitted.");
                    }
            }
    }
然后是ajaxCallback.php:

<?php
    $stringData = $_POST['outputMessage']; 
    echo $stringData;
    $myFile = "logFile.log";
    $fh = fopen($myFile, 'a') or die("can't open file");
    fwrite($fh, $stringData);
    fclose($fh);
?>

ajax
调用中,您的
数据
参数需要使用
键/值
对:

data: {outputMessage:outputMessage}
outputMessage
中,未定义变量
max

使用
fopen
时,还应考虑正确的错误处理:

if($fh = fopen($myFile, 'a')){
    fwrite($fh, $stringData);
    fclose($fh);    
}else{
    die("can't open file");
}

其他一切看起来都很直截了当。

这里不是问codereview问题的地方。。。那是为了