MyBB Community Forums

Full Version: Quick question about MyBB's session handler
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
MyBB has a nice PHP session handler that I have been studying. However, it has made me ask a question. Is MyBB's session handler cached at all? I notice that on every page, it has to update the entry for the current user. My question is, is that not too resource intensive? Or does it not make that big of a difference?
It only costs 1 query to update the mybb_sessions table so it isn't that intensive. The only time it could get problematic is if it is a forum with hundreds of users online at the same time and the mybb_sessions table was using a storage engine of MyISAM or some other one that locks an entire table when writing to it.
Thanks for the explanation. I have one more question. The function that creates the session (create_session) has a strange bit of code that I do not understand. 

On line 474, this code appears:

          // If there is a proper uid, delete by uid.
          if($uid > 0)
          {
               $db->delete_query("sessions", "uid='{$uid}'");
               $onlinedata['uid'] = $uid;
           }


What is the purpose of deleting the entry if you are just going to reinsert it later on? 

$db->replace_query("sessions", $onlinedata, "sid", false);

Is this just to make sure the entry does not duplicate? So therefor MyBB uses 2 queries (which still is not a lot), one that deletes the entry, and then one that reintroduces it. 


It also does not take up that much storage because the daily_cleanup task removes all entries that are older than 24 hours I believe.
If you don't delete the previous entry for a user, when you go to view Who's Online, it will show a person twice. A uid of zero is handled separately than someone with a proper uid. I will say though that a replace query is not the best choice and should be an update query instead.
Wouldn't a replace query be the best? Since mybb_sessions does not use any auto increment value, replacing it basically recreates it. It also has the ability to create it if it does not exist.
(2017-03-29, 11:11 PM)Achilles Wrote: [ -> ]Wouldn't a replace query be the best? Since mybb_sessions does not use any auto increment value, replacing it basically recreates it. It also has the ability to create it if it does not exist.

No, if you update, it's one query vs two to delete and re-create