Capture time that a visitor spends on a page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sleepydad
Forum Commoner
Posts: 75
Joined: Thu Feb 21, 2008 2:16 pm

Capture time that a visitor spends on a page

Post by sleepydad »

I'm designing a page where I need to capture the length of time that a visitor spends there and save that capture to MySQL. Also, I'd like to put a cap on the amount of time that they're allowed - say ten minutes. I'm a relative newbie to PHP, but am guessing these requirements are pretty straightforward. The caveat is that if the visitor returns to that page at a later date, I'd like to capture the time of the new visit and 'add' that to the time already logged in MySQL. Is this possible to do? Could someone direct me to a tutorial or give me the Reader's Digest version on how to accomplish this?

Thanks very much.
sleepydad
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Capture time that a visitor spends on a page

Post by requinix »

There is no guaranteed way of doing it. The best way is so easily circumventable it's hardly worth the effort.

The Flash solution involves more work so somebody else can give it if they want.

With JavaScript you keep a running counter, and when it hits a certain total you make the user go someplace else.
At the same time you use AJAX to update your database every, say, 5 seconds. When the page is requested your PHP does some work and figures out how long the user can be on that page.

view.php (not for IE, because this is just an example)

Code: Select all

<?php
// look up information
// let's say I arrive at the conclusion that:
// a) this user has been on this page for 63 seconds
// b) they can be here until 120 seconds
$left = 120 - 63; // 57
?>
<html>
<body>
<p>You are viewing this page.</p>
<script type="text/javascript">
var ajax = new XMLHttpRequest(); // AJAX support
var counter = 0, max = <?php echo $left; ?>; // viewing time, viewing limit
function updateDB() { // send counter off to the database
    ajax.open("GET", "/counter.php?counter=" + counter, false);
    ajax.send();
}
function updateCounter() { // increment counter
    counter++;
    if (counter == max) { // reached the viewing limit
        updateDB(); // update
        document.location.href = "/nomore"; // redirect
        return; // stop
    }
    if (counter % 5 == 0) updateDB(); // update DB every 5 seconds
    window.setTimeout("updateCounter();", 1000); // execute again in 1 second
}
window.setTimeout("updateCounter();", 1000); // start running in 1 second
</script>
</body>
</html>
counter.php

Code: Select all

<?php
 
$add = (int)$_GET["counter"];
// add $add seconds to the user's viewing time
 
?>
Untested, 40% likely won't work, but the principle is there. Besides that, Google is your friend: important keywords are "php" and "ajax".
Last edited by requinix on Mon Jun 15, 2009 7:32 pm, edited 1 time in total.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Capture time that a visitor spends on a page

Post by califdon »

sleepydad wrote:I'm designing a page where I need to capture the length of time that a visitor spends there and save that capture to MySQL. Also, I'd like to put a cap on the amount of time that they're allowed - say ten minutes. I'm a relative newbie to PHP, but am guessing these requirements are pretty straightforward. The caveat is that if the visitor returns to that page at a later date, I'd like to capture the time of the new visit and 'add' that to the time already logged in MySQL. Is this possible to do? Could someone direct me to a tutorial or give me the Reader's Digest version on how to accomplish this?
Tasairis has given you a good start, if you really want to proceed with this, but I just want to call to your attention the underlying issue, which is that HTTP is a stateless protocol, meaning that it operates on the principle of request-and-response--a browser requests a document from a server; the server sends the document to the browser; that's it, there's no "connection" between the two. At the browser end, we can establish a "session" to hold data, but we don't know whether the user is even still there or not. In other words, the user is in charge! It's easy enough to LIMIT the user, probably with Javascript, if the user's browser doesn't have it turned off, but the only way to know if a user is still viewing the document is to constantly send new requests back to the server, as tasairis suggested--a very bandwidth-intensive operation if many simultaneous users are involved! I believe you can have a Javascript function fire on document.unload(), but there are serious limitations on that, too. I would recommend that you carefully consider the value of measuring "the length of time that a visitor spends." For one thing, what does that mean? If the user goes to the bathroom or leaves his office with the browser still open, do you want to measure that? Again, the Internet is a stateless protocol by design, which means that there is no defined state of "being connected." Anything you manage to do will be an artificial approximation, at best.
Post Reply