views:

50

answers:

3

Hello guys,

I'm trying to set up a cookie which would store the time when the user leaves a website, so in the next visit he can find a "There's new content" message (if any) - checked against his "last seen" stored time.

The things I'm stuck with are two : how do I send a cookie when he leaves the site ? And, then, how do I check that data when he comes back, compared to the last published content's time ?

This is the code I have by now :

<!-- I send the cookie -->

    <?php

        // How long should something be considered new for? (In seconds.)
        // seconds * minutes * hours * days
        // Default is 72 hours (3 days).

        $stillnew = 60*60*24*3;

        setcookie('CookiePublishing', time()-$stillnew, time()+60*60*24*30, '/');

    ?>

<!--I check the cookie and print -->

    <?php

        $entrydate = last_comment_time();

        if ($_COOKIE['CookiePublishing'] < $entrydate) {
        echo '<p>New Comment!</p>';
        }

    ?>  

As you see, I imagine a function called last_comment_time() that I have still to figure out, but my main concern would still be the moment in which the cookie is sent.

Many thanks for any input / alternative solution.

A: 

You have to use javascript.

One way to do this is to have the user's browser ping the server every five minutes or so. If the user doesn't ping the server for 6-7 minutes, he's gone.

edit: To give a bit more detail, what you need to do is send a blind ajax request to the server (so don't bother waiting on a response or anything). The script receiving the request should update the user's "last ping" field in your database or wherever. To find the last time they visited, it's "last ping" + time between pings.

This isn't really necessary if you just want to list new comments, as in that case you could simply record the time the user views the page, and display comments since then the next time the user visits (as FryGuy mentioned).

However the ping method does address your question of finding the amount of time since the user has last visited.

One obvious problem is if the user doesn't have javascript. To minimize the problems caused by this, update the "last ping" with PHP every time the user views the page.

Cam
The ping idea is pretty clever. Nice.
Nathan Taylor
+3  A: 

I don't think it matters when he leaves the site, but rather, what comments are on the page when it's loaded. For example:

12:00 comment1
12:05 comment2
==loaded at 12:10, sees comments 1-2==
12:15 comment3
==leaves site at 12:20==
==arrives again at 1:10==

In this case, he should be notified that there are new comments, since the last time he saw the page, there were only comments 1-2.

FryGuy
Good point, I don't even know now why I was thinking the other way round
Peanuts
Yes, because he could read the comment 3 after some browsing, then receive the message anyway (with or without 4th). But, in any case, that would be better than a lack of notification.
Peanuts
+1  A: 

You may want to consider using PHP's built-in session handling and using $_SESSION.

Anyway, you may need to solve this problem by using two variables: one for the last access time and one for the current access time; when the page is loaded, set the last access time to the current one, then set the current one to the system's time. That way, you can determine content by the value of the last access.

For example:

<?php
    // set cookies
    session_start();
    $_SESSION['lastAccess'] = $_SESSION['currAccess'];
    $_SESSION['currAccess'] = time();
    // send the cookies in the HTTP headers
    session_write_close();
?>

...

<?php
    $last_stillnew = last_comment_time() - 60*60*24*3;
    if ($_SESSION['lastAccess'] < time()-$last_stillnew) {
    print "<p>New comment!</p>";
    }
?>
amphetamachine