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