site wide variables

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
zebrax
Forum Newbie
Posts: 16
Joined: Sat Sep 14, 2002 11:39 pm

site wide variables

Post 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
Tubbietoeter
Forum Contributor
Posts: 149
Joined: Fri Mar 14, 2003 2:41 am
Location: Germany

Post 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.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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.
Jim
Forum Contributor
Posts: 238
Joined: Fri Apr 19, 2002 5:26 am
Location: Near Austin, Texas

Post 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.
Post Reply