I’ve been working on a simple forum for my site. I’m adding a feature that will mark unread posts + forums since your last visit.
Im essentially storing a date of last access to the forum with each user account. When a user comes back to the site, a query is ran to fetch all the post_id and post_parent_forum values, which are then added to an array, that’s stored in a SESSION variable with the key being the post_id (which is unique) and the value being the forum id (which wont be unique since posts will appear in the few forums).
In the forum index, I use in_array() for each forum, to see if the forum_id is in the array, if it is, it will be marked as “unread”.
In the thread listing, I use array_key_exists() to see if the key for each thread ID is in the array, if it is, its marked as “unread”.
When a post is viewed, the item is removed from the array with the key equal to the ID of the thread.
Is this method reasonable, or am I going to run into issues if the forum becomes more popular? Im concerned about running 20 array_key_exists() checks on each forum listing. Is it reasonably fast?
On a side note…. can I work directly with the SESSION stored array, or do I have to assign its value to a regular variable, remove a key, unset the old session var, and re-set it with the updated array?
php arrays are hashtables! a hash key lookup is not very expensive, so no, i don’t think you’ll run into performance problems because of this.
in_array is a different matter (searches through the whole array), but really – it still shouldn’t be a problem. beware of premature optimization!
yes.
$_SESSIONis (normally) just an array that is populated when you callsession_start()by unserializing the session files content. when the script ends orsession_write_close()is called, the $_SESSION array isserialize()‘d back to the file. no magic, really.if you should do that is another question, after all
$_SESSIONis a global variable (ewwww).