Page 1 of 1

database, sessions or includes?

Posted: Tue Jun 21, 2011 1:36 pm
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!

Re: database, sessions or includes?

Posted: Tue Jun 21, 2011 4:12 pm
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.

Re: database, sessions or includes?

Posted: Wed Jun 22, 2011 11:18 am
by tr0gd0rr
Yes. In summary, keep user-specific data in session and keep general data in the database.