• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 630
  • Last Modified:

how long was visitor on webpage

length of time (in seconds) of a page visit

using javascript or php or jquery
3 Solutions
Julian HansenCommented:
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

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!
Ray PaseurCommented:
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!
rgb192Author Commented:
thank you for post form and advice about google analytics
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now