PHP Forum - New Posts

They have: 38 posts

Joined: Jun 2008

Hello All!

I am just coding a custom forum for a website I am developing and I am stuck on one aspect.

How do you detect if there has been any new posts since that users lasts visits.

I am sure there are many ways and have thought of a few myself but they just don't seem to be right, if that makes sense.

How do the popular forums do this? Any help would be greatly appreciated.

Thank you!

S33ker.

greg's picture

He has: 1,581 posts

Joined: Nov 2005

You can use a database or cookies for this.

Cookies is probably less site resource usage, although people can clear their cookies, a lot using Firefox probably do.

The database approach I guess everything would start as unread, then you'd mark something as "read" when they've seen it.

They have: 38 posts

Joined: Jun 2008

Thanks for the reply, thats one way I thought of. But then you would have to have a database that knew if everyone had read every thread in the database. That seems a bit insane to me. Another way I thought of doing it would be storing the last logout of a user then if a thread had been made after the user last logged out then it was obviously new.

Any more ideas?

Thanks Smiling

greg's picture

He has: 1,581 posts

Joined: Nov 2005

S33ker wrote:
But then you would have to have a database that knew if everyone had read every thread in the database. That seems a bit insane to me.

To me too, but it seems the most reliable and accurate approach.

The problem:
You need to somehow show when a user has read or not read a forum post/thread.
This is for all users.

The Solution:
Store this information **somewhere** so the scripts can retrieve it and reflect "read" or "unread" per user per thread/post.

So whatever you do, you need to store it. Cookies are fine, but forum software stopped using cookies as people clear this data too much these days. Many use Firefox and set it to do it when closing the browser. And there's a lot of software, some free, to "clean up" one's PC.

So if you want it reliable and accurate, you are down to either storing it in a File, or a Database.
A file is a messy and complicated approach, and in this scenario I would guess a lot slower than a DB.

Which leaves the DB as the best option. Just make sure you design the DB and table structure correctly.
There are regulars on the forum who have vast knowledge in this area, hopefully will advise for the table setup (and possibly confuse you in the process).

S33ker wrote:
Another way I thought of doing it would be storing the last logout of a user then if a thread had been made after the user last logged out then it was obviously new.

I don't see this as an improvement on just storing what all users have read, as you'd have to get last login and then search DB for all threads made after last login. And what happens to the post made while the user is logged in that they haven't read? You'd still have to store that info somewhere.
Also, knowing if a user is logged out or not isn't always accurate, or even possible.
If they just close their browser, and depending on your forum setup, they haven't actually logged out. They will only be logged out by clicking logout or when (if) the session/cookies has an expire time.

It's much more simple (for quick example) to just have a separate table that stores user ID and thread/post ID they have read.
Then show all as unread that is not stored in there and update it when they read a thread/post marked unread.
When they login you could keep the list of all unread at that point in a session or (better) a temp DB table so each page/thread view you are not going back into the DB and getting the entire list again, for all users for all post/thread views, as this would likely be a resource issue.

They have: 38 posts

Joined: Jun 2008

Excellent information thank you ever so much, lots to think about! Smiling Will post once it has been finished or if I get stuck again Smiling

Thanks again!

S33ker.

They have: 38 posts

Joined: Jun 2008

Hey all again,

I would just like to update you on the method I have used, see what you think of it.

Each thread has a last post time, so every time a new posts is made under that thread this time is updated.

I also store when a user has read a thread and the time they read it, every time they read it, this time is also updated.

So to work out if there are new posts I just do, if($newPostTime >= $readPostTime) then there are new, unread posts. Smiling

What do you think?

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.