tags:

views:

141

answers:

5

On some websites or forums I usually go to, entries that I havn't read yet are marked as "new". I would like to implement this feature on a website I have developed (with news being posted from time to time) -- php/mySQL. How is this usually done ? Does it require using a cookie remembering the last visit date/time ? Or the last viewed posts/urls ? How can I identify the users ?

A: 

Cookie is about the only reliable way to do this type of thing.

I'd use a cookie to store when a user last visited, but also have a reasonable default of say 1 week if the cookie doesn't exist.

Then show new against things newer than that date.

benlumley
A: 

You can either store the actual last visited date in a cookie, or you can just store a unique id for that person in a cookie and track what they last read in the database. If you do the latter, you can allow them to log in with the same id on different browsers and still get an accurate count.

Paul Tomblin
+3  A: 

Cookies are just one possible way of identifying the user for the session, or between visits for those without authentication. Though a very common and useful way. (PHP can also use the sid or another parameter, though its not common anymore.)

You need to store either which threads/posts the user have read, or which he/she has not. You can sum things up with read everything before 'date' or postId for certain subforums.

This all depends on the lay out of your forums, posts and news, and how dynamic they are. You might also only want to show new posts since last visit, show new posts while the user is currently at your site, then use the new posts since last visit if the user is away for more then a predefined (x hours)/calculated (y hours if weekend, z hours if admin) time.

Edit: CSS for visited links will not help you with new comments for news, new posts in a thread, go directly to the first unread post or accessing the site at work/school and home.

OIS
That's very insightful ! Thank you !
RaGE
A: 

I do it this way:

  • If the user doesn't have a username and isn't logged in, then I don't show new items.
  • If the user is logged in, and they have been shown all items then I store the current date/time on the user file, and use this value to work out what items are new.
  • If the user date/time is within the last ten minutes then I don't update the user date/time.

Here's my code...

function drawPage
    if (isLoggedIn) 
        get dbUser from database
        lastUserDateTime = dbUser.LastCommentTime
    else 
        lastUserDateTime = yesterdaye
    end if

    for each post
        get date of post 
        if post->date < lastUserDateTime mark it as new
        draw the post
    loop

    if (isLoggedIn) 
        if (lastUserDateTime + 10 mins) < now 
            dbUser.LastCommentTime = now
            update dbUser in database
        end if
    end if
end function
seanyboy
+3  A: 

A side note: doing so would actually be duplicating browser functionality: (as long as you use fixed urls) the browser will give links a different style on visited links. Of course this will reset if you clear history, but then again a cookie only based solution will allso clear if the cookie is deleted (with many browsers having a "delete private data" function wich deletes both by default, it would usually be reset at the same time)...

This site have fixed url's (for questions) and doen't set the visited color the same as normal link color, so you can see the questions you've visited by the link color.

EDIT: You can also use CSS to add an icon to visited links.

Stein G. Strindhaug
Thank's Bill, I was actually thinking about that when I started writing, but I somehow forgot... ;)
Stein G. Strindhaug
How could I not think about that ! Thank you very much for the input !
RaGE