Page 1 of 1

site wide variables

Posted: Wed Apr 02, 2003 8:51 pm
by zebrax
I am starting a new project and I want to store all variables (site name, colors,etc) in the database. I have done this before and it works great.

My question is what is the best way to get the variables out of the db.
I can think of a few ways.

1) use defines to and loop through all of the variables in the db.

2)use variable variables and loop through all values in db.

3) use a function like get_variables and call that every time a variable is needed.

Which method is best , to a)keep the script fast and b)minimize impact on db server.

Thanks

Posted: Wed Apr 02, 2003 11:20 pm
by Tubbietoeter
I'd try to get the values only once when a session is started. store them in session variables. if the settings are being changed, i'd call a function that writes the values back and re-gets the variables.

Posted: Thu Apr 03, 2003 9:54 am
by McGruff
I design most of my sites with a single index.php "gateway" in which all the site pages are included. Any common values can be defined here for the whole site.

If you don't have a "gateway" file, you could create a php file with your constants / variables and include wherever you need it. More efficient than doing a query.

I prefer constants over vars since I think they're a little more secure.

Posted: Thu Apr 03, 2003 2:37 pm
by Jim
Agreed, McGruff.

When creating specific pages, I used to include a config file at the top of each page to ensure that I was:

a.) Always connected to my MySQL DB, since most of the programs I've written have required DB access to run.

b.) Done some simple user authentication using cookies, because most of the programs I make require some form of membership to allow access.

c.) Defined any variables I would need later on in the script, especially ones like $username, $password and $email.