我今天一直在编写以下代码,它在我的本地机器上运行良好。但是当我在我的 iPhone/iPad 上测试它时,它根本不起作用。
该代码应该在用户离开网站时将页面访问的持续时间保存到文本文件中。我知道互联网上有很多可供下载的替代脚本,但我想自己写这个。欢迎任何建议。
索引.php:
<!DOCTYPE html>
<html>
<HEAD>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript">
function unloadPage(){
var dd = new Date();
var hh = (dd.getHours()<10?'0':'') + dd.getHours();
var mm = (dd.getMinutes()<10?'0':'') + dd.getMinutes();
var ss = (dd.getSeconds()<10?'0':'') + dd.getSeconds();
var htmlString="<?php echo date('Gis') ?>";
var timed = (hh + "" + mm + "" + ss) - htmlString;
jQuery.ajax({
url: 'time2.php?t=' + timed,
success: function(result) {
if(result.isOk == false)
alert(result.message);
},
async: false
});
}
window.onbeforeunload = unloadPage;
</script>
<meta charset="utf-8">
<title>thepaintjob.nl</title>
</head>
<BODY>
<?
$myFile = "temp.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, 5);
fclose($fh);
echo duration($theData);
?>
</body>
</html>
<?php
function duration($secs)
{
$vals = array('w' => (int) ($secs / 86400 / 7),
'd' => $secs / 86400 % 7,
'h' => $secs / 3600 % 24,
'm' => $secs / 60 % 60,
's' => $secs % 60);
$ret = array();
$added = false;
foreach ($vals as $k => $v) {
if ($v > 0 || $added) {
$added = true;
$ret[] = $v . $k;
}
}
return join(' ', $ret);
}
?>
时间2.php:
<?php
$myFile = "temp.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, 5);
fclose($fh);
echo $total = $theData+$_GET['t'];
$File = "temp.txt";
$Handle = fopen($File, 'w');
$Data = $total;
fwrite($Handle, $Data);
fclose($Handle);
?>