Home > Backend Development > PHP Tutorial > php 如何时间观察访问者 停留页面的时间

php 如何时间观察访问者 停留页面的时间

WBOY
Release: 2016-06-23 14:17:15
Original
973 people have browsed it

这个大虾~

有啥函数来监控用户关闭页面或者离开页面这个动作?


回复讨论(解决方案)

这是客户端的事

网上找到一款 php 结合 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> 
Copy after login



log.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\r\n";     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);     } }
Copy after login

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template