How to Record page dwell time in PHP _php tips

Source: Internet
Author: User

This article gives an example of how to record the time of a page in PHP. Share to everyone for your reference, specific as follows:

First in the page to be monitored add JS 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?stay_ms= ' + ms;
}
</script>

The log.php code is as follows

<?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);
  }
}

More interested in PHP related content readers can view the site topics: "PHP Network Programming Skills Summary", "PHP basic Grammar Introductory Course", "PHP operation Office Document Skills Summary (including word,excel,access,ppt)", " PHP Date and Time usage summary, PHP Introduction to object-oriented Programming, PHP String (String) Usage summary, PHP+MYSQL database Operations Tutorial and PHP Common database operating Skills summary

I hope this article will help you with your PHP programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.