Skip to menu

Skip to content



Using a global configuration file

Friday, December 5th, 2008 at 3:29am, in Misc, written by Stefan Ashwell

It's one of the basics of PHP and if you read any kind of tutorial or book on the subject it's one of the first things they cover, yet I still see people no using a global 'configuration' or 'include' file. It's one of the easiest things to do and will make your life so much easier!

This 'configuration' file will include anything that needs to be run on every page of the website. This could be database connections, creating classes that are used everywhere on the site, checking for user sessions, initiating a template engine etc. These things that are required for every script in order for them to run correctly, but we don't want to have to include the same code on tens or even hundreds of scripts now do we?

We can simply put all this in a separate file and then 'include' it on every script at the top, so it's all done and dusted. We can do this using the include function like so:

include 'config.php';

Now the common code will run as if it was on the page itself - nice and easy! Now if there's a bug or we need to update some of the common code we only have to do it once!

Remember you can do this to create headers and footers or columns that are common across your site too if you use PHP for your templating, and template engines like smarty have their own include functionality so you can create global templates.

I hope you've found this article useful, if you have any comments don't hesitate to leave one!

Share and Enjoy:

  • bodytext
  • del.icio.us
  • Facebook
  • Google
  • Reddit
  • Technorati
  • Furl
  • StumbleUpon
  • NewsVine
  • Slashdot
  • Spurl
  • Ma.gnolia
  • Live
  • YahooMyWeb
  • E-mail this story to a friend!

Subscribe to Total PHP: RSS | Email

Related Posts:

- Using an autoload function to make your life easier

- Choosing a PHP Web Host

- Why you will love PHP5

There are no comments on this article yet

Please log in to post a comment about this article.


Subscribe to our RSS Feed

Subscribe: RSS | Email

Clear Content - Easy to use PHP 5 Content Management System
CSS Contest

Recommended resources

Recent articles