how long was visitor on webpage

Posted on 2012-09-02
Last Modified: 2012-09-03
length of time (in seconds) of a page visit

using javascript or php or jquery
Question by:rgb192
    LVL 49

    Accepted Solution

    Here is some PHP code using sessions - can also save this information in a cookie - post back if you want that code - this would need to be included on every page.
    if (isset($_SESSION['last_visit'])) {
       $time = time() - $_SESSION['last_vist'];
       // Then either get page from session or use HTTP_REFERRER
       $page = empty($_SESSION['last_page']) ? $_SERVER['HTTP_REFERRER'] : $_SESSION['last_page'];
    // Do something with this information here.
    $_SESSION['last_visit'] = time();
    $_SESSION['last_page'] = $_SERVER['URL'];

    Open in new window

    However this is dependent on visitors returning to your site - another option is to put some javascript (Google Analytics style) into your page that makes a callback when the page loads and when the page closes.
    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

    // 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;

    Open in new window

    LVL 2

    Assisted Solution

    You are asking for a customized solution, but let me suggest you to use Google Analytis, which is extremely easy to integrate and gives you a huge number of information, not only the duration of visits, but also the geo-location of visitors, browser used, operating system, language,  and many many more.

    you could start here to setup your account:

    I've tried many customized solutions before and returned to Google Analytics because  simply he's the best.

    Good luck!
    LVL 107

    Assisted Solution

    by:Ray Paseur
    Agree with fourck: Google Analytics is the correct tool to use.  However there is no tool that can really tell you how long a user was "on" a page.

    The reason you cannot know goes to the nature of client-server systems.  In the HTTP protocol, the client makes a request and the server makes a response.  The request is atomic, complete and stateless, and the response is complete and usually instantaneous.  And that is all that happens.  Once the server has sent the response, it disconnects and waits.  Presumably the client will be sitting at a computer looking at the web page.  Or maybe not - you can't tell from the server side.

    As an example, consider this web page.  You know I was here once before because you can see that I left information in the forum postings.  But where am I now?  Am I still looking at this page or have I left to go get some coffee?  Even if you're going to the trouble of using some kind of AJAX polling mechanism, all you can really get is the time that the client first called the server and the time that the client last called the server.  The human client might open your web page and then open another window, leaving your web page unattended, perhaps for long periods of time.  Or there might be many instances of the browser looking at your site, from the same IP address.  This can happen if I have more than one computer accessing the internet from an office environment.

    So I would just live with the measurements that are provided by Google Analytics rather than trying to make something that is somehow better than GA.  It is the industry standard and if GA says that clients were on a page for an average of 5 seconds, you can use that answer.  If you write your own, you will have to defend the time-on-page measurement, and it's mostly indefensible!

    Author Closing Comment

    thank you for post form and advice about google analytics

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
    Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to dynamically set the form action using jQuery.

    760 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

    7 Experts available now in Live!

    Get 1:1 Help Now