这是一个最小的例子 -
索引.php
<?php
$count = file_get_contents("count.txt") + 0;
file_put_contents("$count.txt [loaded]", '');
file_put_contents("count.txt", $count + 1);
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<script src="jquery-1.10.2.min.js"></script>
</head>
<body>
<main>
<p> hi there </p>
</main>
<script type="text/javascript">
var id = "<?php echo $count; ?>";
</script>
<script src="script.js"></script>
</body>
</html>
脚本.js
$(document).ready(function ()
{
$(window).bind('beforeunload', function () {
$.post("unloader.php", { id : id });
});
});
卸载程序.php
<?php
file_put_contents("$_POST[id] [unloaded]", '');
当我打开网页时,会创建一个以计数为名称的文件。当我关闭选项卡 jquery requests unloader.php 时,它只是一个独立的脚本,它也创建了一个以计数为名称的文件。
有时有效,有时无效。我的意思是始终创建打开文件。但有时没有制作必须在关闭时创建的文件。知道问题出在哪里吗?