How to determine new posts for a thread?

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
Infamy
Forum Newbie
Posts: 8
Joined: Sat Feb 18, 2006 10:04 pm

How to determine new posts for a thread?

Post by Infamy »

Hello,

As you can tell from my previous post, I have not been actively coding PHP in quite some time. However, I'm getting back into it.

When I stopped coding I was working on a forum. Well, I have revisited that project.

I am working on a site and have modified the forum software to work with this current infrastructure, which was fairly similar to the one it was originally written for.

One of the main issues I've had --and had even when I was writing it originally-- was how to determine new posts.

I considered many different options, but decided I'd similar compare the time of the last post and such.

I'm storing all times in the database with time() (and I know there is some controversy about that). When the page is loaded, the functions.php scripted is loaded and this is code that is evaluated:

Code: Select all

$_lastvisit_min = 4;

$forum_lastvisit_time = time() - (60 * $_lastvisit_min);

....

if ($issession) { 
     global $session;
     $update_session_forum = $DB->query("UPDATE sessions SET forumlastvisit='".time()."' WHERE sessionhash='".addslashes($session['hash'])."' AND userid='".addslashes($user['userid'])."' LIMIT 1");
     if ($user['forumlastvisit'] < $forum_lastvisit_time) { 
         $DB->query("UPDATE login SET forumlastvisit='".time()."' WHERE userid='".$user['userid']."' LIMIT 1");
      } 
  }
Now this DOES work, but when the user logs back in after some time of inactivity, it is (obviously) resetting their time. So whenever you view the forums after logging back in after a couple minutes of inactivity, there are no new threads because the above script is updating the user's database row.

So I need to figure out a way to determine a certain amount of inactivity and not upload the script right away if that condition is met. I'm not great when it comes to math and equations, so this has been rather difficult for me, which is compounded by the fact that I've been away from PHP for such a long time.

I've considered using cookies to track the topics and I've also considered having a database table that simple keeps track of it all and deletes it when a new post is added, but there'd be an awful lot data almost needlessly stored in the database.

I've also considered just checking the forumlastvisit variable and multiplying it by like ten minutes. If the user has not logged in for at least that amount, it doesn't update the time until after they have been given ample time, but then I can't think of how to get out of that condition!

Does anyone have any ideas on how I can do this, or of other ways I can efficiently keep track of thread views?

I don't need it to be insanely accurate, but I've tried several ideas to not reset the time and I've even considered just keeping it how it is now. But I'd even be a little annoyed that everytime I came back to the forum and knew there were posts but the forum was specifically telling me that there were not.

I'm sure there is a simple solution that is simply escaping me, but I've been struggling with this. :(

Thanks.

P.S. I believe this is the aspect of the script that frustrated me so much that I quit coding! lol.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

you could set cookies...

dunno if you wanna go that route though
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Infamy
Forum Newbie
Posts: 8
Joined: Sat Feb 18, 2006 10:04 pm

Post by Infamy »

scrotaye wrote:you could set cookies...

dunno if you wanna go that route though
Yeah, I did consider that and I did try to use the session control with PHP to figure something out. But I'd like to avoid using cookies as much as possible.

I have tried so many different things to get this thing to work. The script I posted above is so simple compared to the one I was using before. lol..
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Here's how phpBB does it: there are two times at work here. The first is a real timeout (default is two hours or new login), second is an "online" which merely checks to see when you were last heard from (in your session).

So in the users table there are two timestamps for this. The first being the last time seen and the second being the last visit.

When a login or two hours has passed since last seen, the last visit is updated with that time.

So roughly, you can do that in a single update query (this is not checked for syntax or correctness):

Code: Select all

UPDATE `table`
SET `lastVisit` = IF(`lastSeen` - `lastVisit` > 7200, `lastSeen`, `lastVisit`),
 `lastSeen` = UNIX_TIMESTAMP(NOW())
WHERE `user_id` = 'xyz'
Infamy
Forum Newbie
Posts: 8
Joined: Sat Feb 18, 2006 10:04 pm

Post by Infamy »

I'm having some issues understanding. lol.

This is what I have now:

Code: Select all

$DB->query("UPDATE sessions SET forumlastvisit='".time()."' WHERE 

sessionhash='".addslashes($session['hash'])."' AND userid='".addslashes($user['userid'])."' LIMIT 1");


    $DB->query("UPDATE login SET forumlastvisit='".time()."' WHERE userid='".$user['userid']."' LIMIT 1");


    $DB->query("UPDATE login SET forumlastvisit = IF(`forumlastseen` - `forumlastvisit` > 7200, `forumlastseen`, `forumlastvisit`), forumlastseen = UNIX_TIMESTAMP(NOW()) WHERE userid='".$user['userid']."' LIMIT 1");
Should I actually be updating forumlastseen, rather than forumlastvisit, in the second query?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The second query does both. So you shouldn't need the first query.
Infamy
Forum Newbie
Posts: 8
Joined: Sat Feb 18, 2006 10:04 pm

Post by Infamy »

Blah. Hahaha.

Thank you. I did not notice that, because I'm a fool. lol.
Infamy
Forum Newbie
Posts: 8
Joined: Sat Feb 18, 2006 10:04 pm

Post by Infamy »

Although my preliminary testing seems to indicate that it's working properly (changing 7200 to 1 and reloading- haha!), I will work with this. Thank you once again.
Post Reply