"Since your last visit" - how is this done?

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
Donny Bahama
Forum Newbie
Posts: 18
Joined: Wed Mar 22, 2006 2:48 pm

"Since your last visit" - how is this done?

Post by Donny Bahama »

Application is a simple message board. Each time the user returns I want to show a message stating the number of new posts since the last visit.

I know I could do this in a cookie or in a database field, but what's tripping me up is the general logic...

Let's say the last post (ID) that the user saw was 1122. Now the user returns and MAX(ID) is 1134. This is an easy query; no problem. But let's say I want to maintain the knowledge that posts 1123-1134 are new to this visitor until the visitor leaves, at which point, I want to know that 1134 was the last/highest post he may have seen.

I can't set $LastPost every time he hits the MsgList page, because if I do, I lose track of the fact that posts 1123-1134 are new to this visitor. And I can't really set LastPost once he's gone because I can't really know he's gone unless I force him to log out (which I don't want to do).

I know there must be a simple way to do this, but darned if I can figure it out.
Any help on this will be much appreciated!
bdlang
Forum Contributor
Posts: 395
Joined: Tue May 16, 2006 8:46 pm
Location: Ventura, CA US

Post by bdlang »

Now, I can't speak for how the big forums are done, but I do this with a timestamp for each post, and one on the `user` table or `logins` table. Find all posts that were made / changed between the time the user was last seen and now. Store the user's last access timestamp value in both a cookie and the `user` or `logins` table, check for cookie first, barring that, do a JOIN using the timestamp value from both tables. After you've retrieved that data, update the user's timestamp.

Obviously you have to have a timestamp on all tables that can be updated via each INSERT / UPDATE mechanism.
Donny Bahama
Forum Newbie
Posts: 18
Joined: Wed Mar 22, 2006 2:48 pm

Post by Donny Bahama »

I apologize, I don't think I stated my question very clearly. (I knew this was going to be tricky!)
bdlang wrote:I do this with a timestamp for each post, and one on the `user` table or `logins` table. Find all posts that were made / changed between the time the user was last seen and now. Store the user's last access timestamp value in both a cookie and the `user` or `logins` table, check for cookie first, barring that, do a JOIN using the timestamp value from both tables.
Determining which posts are new and where to store the datum isn't the issue. That stuff I can handle. The part I don't get is how to retain that information for the user's entire session, only updating the "LastPost" datum at the end of the session.
After you've retrieved that data, update the user's timestamp.
But, see, that's just what I'm trying to avoid. I want the user to be able to continually refer back to the (query driven) page that shows him the new posts since his last visit... but if the query uses the LastPost datum (be it a timestamp or a post ID or whatever) as its filter parameter, then once I update that datum, I change what the query returns, effectively losing the data I wanted to display!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You need to ask how you manage their 'last visit time'. Then you take a count of all things that are dated after that date until now.

In many cases this is done by using the last known activity time stored in the users profile as their session information. Basically, if you were to make a post here today then close your browser, then come back in five days, the app would know on your next log in that the last time you were here was five days ago when posted something. Then it can count all the things that happened since that time and tell you how many things there are.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Donny Bahama wrote:
After you've retrieved that data, update the user's timestamp.
But, see, that's just what I'm trying to avoid. I want the user to be able to continually refer back to the (query driven) page that shows him the new posts since his last visit... but if the query uses the LastPost datum (be it a timestamp or a post ID or whatever) as its filter parameter, then once I update that datum, I change what the query returns, effectively losing the data I wanted to display!
This is a little different from what I just posted because you want to keep a clean view for the user. You may have to make a new table that keeps this information.
Post Reply