database, sessions or includes?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
gymBob
Forum Newbie
Posts: 3
Joined: Mon Jun 09, 2008 10:24 am

database, sessions or includes?

Post by gymBob »

I am fairly new to PHP, so I have a basic question. I am developing a website and need variables set once to be used over and over again on all pages. My question is what's the best way to do this? I've come up with three solutions on my own but would like another opinion with more experience. Here are my solutions so far:

1. Read the data from a MySQL database on each page.
2. Read data from MySQL on one page and use sessions to pass the data to all other pages.
3. Set variables in an include file and include it on each page.

Which would be faster to the client? Which would be more efficient as far as server load? Am I missing a better solution? Any help would be appreciated.

Thanks!
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: database, sessions or includes?

Post by Christopher »

Well the difference between #1 and #3 is just where the data is stored. #3 will be faster than #1, but if you or users need to update the data often then a database makes more sense. #2 is a way to cache values from a slower datasource in the session for use during the lifespan of the session. Note that if users can change these values they would need to be written back to both the session and database.

Typically, values that are configuration information that rarely change are included. Values that change regularly or is added/managed by users is stored in a database. And the session (or cookie) is used for values that are needed only during duration of a user's visit to a site, such as login verification, shopping cart contents, etc.
(#10850)
User avatar
tr0gd0rr
Forum Contributor
Posts: 305
Joined: Thu May 11, 2006 8:58 pm
Location: Utah, USA

Re: database, sessions or includes?

Post by tr0gd0rr »

Yes. In summary, keep user-specific data in session and keep general data in the database.
Post Reply