Measuring online time on website

后端 未结 7 387
广开言路
广开言路 2021-01-24 05:01

I would like to measure how much time a user spends on my website. It\'s needed for a community site where you can say: \"User X has been spending 1397 minutes here.\"

A

7条回答
  •  隐瞒了意图╮
    2021-01-24 05:39

    A very simple solution is to use a hidden iframe that loads a php web page periodically. The loaded web page logs the start time (if it doesn't exist) and the stop time. When the person leaves the page you are left with the time the person first came to the site and the last time they were there. In this case, the timestamp is updated every 3 seconds.

    I use files to hold the log information. The filename I use consists of month-day-year ipaddress.htm

    Example iframe php code. Put this in yourwebsite/yourAnalyticsiFrameCode.php:

     $line)
        {
          echo($line);
          if ($count == 0)
            $startTimeDate = rtrim( $line );
          $count++;
    
        }
      }
    
      if ($startTimeDate == "")
        $startTimeDate = date('H:i:s d-M-Y');
    
      $endTimeDate = date('H:i:s d-M-Y');
    
      // write the start and stop times back out to the file
      $file = fopen($clientFileRecord,"w");
      fwrite($file,$startTimeDate."\n".$endTimeDate);
      fclose($file);
    
    
    ?>

    The javascript to periodically reload the iframe in the main web page.:

    
    

    The iframe in the main web page looks like this:

    A very simple way to display the time stamp files:

    
    
    
    
    
      
    
    
    
    Analytics results
    
    \n"); $lines = file($folder."/".$fn); foreach ($lines as $line_num => $line) { echo(" ".$line."
    \n"); } echo ("
    \n
    "); } ?>

    You get a results page like this:

    22-Mar-2015 104.37.100.30

    18:09:03 22-Mar-2015

    19:18:53 22-Mar-2015


    22-Mar-2015 142.162.20.133

    18:10:06 22-Mar-2015

    18:10:21 22-Mar-2015

提交回复
热议问题