Home  >  Article  >  Backend Development  >  How to record page dwell time in PHP, php page dwell time_PHP tutorial

How to record page dwell time in PHP, php page dwell time_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 08:55:17747browse

How PHP records page dwell time, php page dwell time

The example in this article describes the method of PHP recording page dwell time. Share it with everyone for your reference, the details are as follows:

First add JS to the page you want to monitor as follows

<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 code is as follows

<&#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);
  }
}

Readers who are interested in more PHP-related content can check out the special topics of this site: "Summary of PHP Network Programming Skills", "Introduction Tutorial on PHP Basic Grammar", "Summary of PHP Office Document Skills (including word, excel, access, ppt)", "php date and time usage summary", "php object-oriented programming introductory tutorial", "php string (string) usage summary", "php mysql database operation introductory tutorial" and "php common database operation skills summary" 》

I hope this article will be helpful to everyone in PHP programming.

Articles you may be interested in:

  • Solution to setting page timeout in php
  • A simple method to calculate page loading time in php
  • Timer implemented by php Page running time monitoring class
  • Introduction to functions for calculating PHP page running time
  • Analyzing the implementation code of PHP calculating page execution time
  • Getting PHP page execution time and database read and write times , the number of function calls, etc. (THINKphp)
  • PHP a page execution time code
  • php page execution time calculation code

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1117061.htmlTechArticleHow PHP records page dwell time, php page dwell time This article describes the method of PHP recording page dwell time. Share it with everyone for your reference, the details are as follows: First of all...
Statement:
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