首先在要监控的页面添加JS如下
<script> var dt1 = new Date(); window.onbeforeunload = function(){ var dt2 = new Date(); var ms = dt2.getTime() - dt1.getTime(); var img = new Image(); img.src = 'log.php?stay_ms=' + ms; } </script>
log.php 代码如下
<?php $refer = ''; if (isset($_SERVER['HTTP_REFERER'])) { $refer = $_SERVER['HTTP_REFERER']; } $user_agent = ''; if (isset($_SERVER['HTTP_USER_AGENT'])) { $user_agent = $_SERVER['HTTP_USER_AGENT']; } $ip = $_SERVER['REMOTE_ADDR']; if (isset($_GET['stay_ms'])) { $log = '[' . date("Y-m-d H:i:s") . '] ' . $ip . ' ' . $refer . ' @ ' . number_format($_GET['stay_ms']) . "ms "; file_put_contents("log/log_" . date("Y-m-d") . ".txt", $log, FILE_APPEND); } if ($_SERVER['QUERY_STRING'] == '' || isset($_GET['day'])) { $day = isset($_GET['day']) ? $_GET['day'] : date("Y-m-d"); $file = "log/log_" .$day . ".txt"; if (file_exists($file)) { $log = file_get_contents($file); echo nl2br($log); } }
以上就是php 记录页面停留时间的方法代码实例的详细内容,更多请关注二当家的素材网其它相关文章!
友情提示:垃圾评论一律封号 加我微信:826096331拉你进VIP群学习群