Session or Database?

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
Zander1983
Forum Newbie
Posts: 20
Joined: Mon Mar 21, 2011 2:26 pm

Session or Database?

Post by Zander1983 »

Hi

A part of my site allows users to send messages to other users. When a member is logged on, they see a panel on the left with a link to the messages page. If there is a message they have not seen, it looks like messages(1). As this panel is on every page, the message(1) is displayed on every page.

My question is a general one which i've always wondered about - I determine whether all messages have been read or not from the database. Should I go once to the database when user logs on, and save this value to a session, or should i go to the database each time the member goes to a new page....

The reason I ask is because I am saving a lot of data in the session already so where do I draw the line between saving stuff to a session and just repeatedly going to the database..
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: Session or Database?

Post by danwguy »

if you save the value in session, you would have to update session if they read that message, otherwise the value wouldn't update until they logged out and logged back in. I would think it would be easier to just check the database, session is helpful but you would have to be updating session values if they read it, plus if they get a message while they are online they wouldn't know about it because their session value doesn't update until you update it.
Zander1983
Forum Newbie
Posts: 20
Joined: Mon Mar 21, 2011 2:26 pm

Re: Session or Database?

Post by Zander1983 »

i see what u mean. i have it set up so it checks the db for messages for each page request. i will leave it like this
Post Reply