length of time on a website

Posted on 2012-09-04
Last Modified: 2012-09-08
I do not want to use google analytics for this project

how can I discover how long (in seconds) user is on website

// assume sanitation ....
$action = $_POST['action'];
$page = $_POST['page'];

if ($action == 'stop') {
   $start = $_SESSION['last_time'];
   $page = $_SESSION['last_page'];
   $visit_time = time() - $start;
   // do something with values
else {
   $_SESSION['last_time'] = time();
   $_SESSION['last_page'] = $page;

echo '<br>session last_time: '.$_SESSION['last_time'];
echo '<br>session last_page: '.$_SESSION['last_page'];
echo '<br>visit time '.$visit_time;


Open in new window

<script type="text/javascript">
var page = window.location; // Or you can set this with a php script
$(function() {
  $.post('timestamp.php', {page: page, action: 'start'});
  $(window).unload(function () {
      $.post('timestamp.php', {page: page}, action: 'stop');


Open in new window

I want to see how many seconds the user is on the webpage

I visit timestamp.php
and see only session_last_time (which increases)

I visit timestamp2.php
and there is no output
Question by:rgb192
    LVL 37

    Accepted Solution

    Personally I would discount every report that ever told me how long a user spends on a web page as meaningless.
    Just because a page is open in a web browser does NOT mean that it is being read.
    LVL 107

    Assisted Solution

    by:Ray Paseur
    Agree with Neilsr, 100%.  The notion of whether a client is logged in, or how long a client is "on" a web page is simply a fantasy.  You cannot know this in any meaningful way.  In the client-server environment, clients make requests and servers make responses.  It is a stateless protocol.  There is no way to know what you're asking for, and Google Analytics gives you at best an approximation of the time from page-to-page within a given site.  Here is an example:

    You visit and the Google web page appears on your computer.  Then you get up, go into the kitchen and get some coffee.  Your wife comes to the computer, visits Yahoo, then brings the Google page back up.  When you get back into the office, you make a Google search for something.

    Here is what just happened on your side (the client side, in client-server).

    1. Client made a request for Google
    2. Client made a request for Yahoo
    3. Client made a request for Google
    4. Client made a request for Google

    In each case, a server responded to the request with a web page, then the server disconnected.  When the server has made its response, its work is complete.  It no longer knows anything about the client.  It does not know whether you are reading the page, whether you have gone to get coffee, or whether you have turned off your computer.  The only indication the server might get would occur when the client made a subsequent request for another web page on the same server.  This information can be tracked, but not reliably.  Google Analytics is about as good as you can get.  And of course it is unable to handle the increasingly common situations in which you might have more than one browser instance open to the same site.

    If you dig into the client-server protocol, you will see that there is only a request from the client.  There is never a "goodbye" from the client.  The goodbye signal comes from the server when it disconnects after it has sent the response.  So the utility of the response, and the client's reaction to it cannot be known by the server unless and until the client uses a link in the response page to visit another page.  

    Given that your site (or any site) bounce rate is probably 25% or more, you're dealing with mostly inaccurate data if your application design somehow relies on this kind of information.

    There are some laboratories that use computerized cameras with eye-tracking heat maps to identify how human clients use web sites.  Fidelity Investments has been a leader in this area of research, as has AARP.  They get closer to the answer by studying the client in situ where they can discern patterns of behavior that are simply invisible on the server side.

    Author Closing Comment

    thank you for the insight and explanation

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit ( and similar technologies have enjoyed wide adoption, making it possib…
    Article by: DanRollins
    This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (, we'll extend the program by adding a depth-…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now