Heim  >  Artikel  >  php教程  >  PHP记录页面停留时间的方法,php页面停留时间

PHP记录页面停留时间的方法,php页面停留时间

WBOY
WBOYOriginal
2016-06-13 08:42:421765Durchsuche

PHP记录页面停留时间的方法,php页面停留时间

本文实例讲述了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&#63;stay_ms=' + ms;
}
</script>

log.php 代码如下

<&#63;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']) &#63; $_GET['day'] : date("Y-m-d");
  $file = "log/log_" .$day . ".txt";
  if (file_exists($file))
  {
    $log = file_get_contents($file);
    echo nl2br($log);
  }
}

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP网络编程技巧总结》、《PHP基本语法入门教程》、《php操作office文档技巧总结(包括word,excel,access,ppt)》、《php日期与时间用法总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

您可能感兴趣的文章:

  • php设置页面超时时间解决方法
  • php简单计算页面加载时间的方法
  • php实现的Timer页面运行时间监测类
  • 计算php页面运行时间的函数介绍
  • 解析PHP计算页面执行时间的实现代码
  • 获取php页面执行时间,数据库读写次数,函数调用次数等(THINKphp)
  • PHP 一个页面执行时间类代码
  • php 页面执行时间计算代码
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn