Knowing Website State Using PHP

By  on  

When I create a website, I create an "app-top.php" file to place website settings in. It's just easier to place a bunch of variable is that file, include it at the top of pages, and know that you only need to change one file if a variable value needs to be changed. A good example of this is a business name change -- I don't want to have to do a search/replace on an entire directory (and risk causing errors) if I can just have a $BUSINESS_NAME that I can reference from my app-top.php file.

One setting is use is a $WEBSITE_IS_LIVE variable. Based on the value of this variable, I can tell if the website is up on the live server or my development server. Say the development server address is "[customer].dev.myutilitydomain.com" and the customer's hosting domain is "www.[customer].com". Here' how my PHP script can know if the site is live or not:

$WEBSITE_IS_LIVE = !substr_count($_SERVER['HTTP_HOST'],'myutilitydomain.com');

What settings do I change depending on the value? Many, including:

  • Database Username
  • Database Password
  • Database Host
  • Database Name
  • Email Address (where to send live form email addresses to; if the site is in development, I want them going to me)

Those are just a few variables I manipulate using $WEBSITE_IS_LIVE. The best part about using the PHP code above is that I don't need to change the setting myself all the time -- using substr_count() automates the process.

Recent Features

  • By
    fetch API

    One of the worst kept secrets about AJAX on the web is that the underlying API for it, XMLHttpRequest, wasn't really made for what we've been using it for.  We've done well to create elegant APIs around XHR but we know we can do better.  Our effort to...

  • By
    Designing for Simplicity

    Before we get started, it's worth me spending a brief moment introducing myself to you. My name is Mark (or @integralist if Twitter happens to be your communication tool of choice) and I currently work for BBC News in London England as a principal engineer/tech...

Incredible Demos

  • By
    5 More HTML5 APIs You Didn’t Know Existed

    The HTML5 revolution has provided us some awesome JavaScript and HTML APIs.  Some are APIs we knew we've needed for years, others are cutting edge mobile and desktop helpers.  Regardless of API strength or purpose, anything to help us better do our job is a...

  • By
    Dynamically Load Stylesheets Using MooTools 1.2

    Theming has become a big part of the Web 2.0 revolution. Luckily, so too has a higher regard for semantics and CSS standards. If you build your pages using good XHTML code, changing a CSS file can make your website look completely different.

Discussion

  1. The best programmer is a lazy programmer. This is a very practical example, great!

    Some settings I may include when on the dev-site are switches like:
    CACHING = 0;
    DEBUG = 1;
    ALWAYS_LOGGED_IN_AS = ‘admin’ ;

  2. Great advice David. In a former life, I used to update the settings in a config file, changing them for the test server, the live server and a local verison and every now and again – I’d upload the wrong settings to the live server – whoops! That had to change.

    Now I use a $MODE variable so $MODE = live or $MODE = test. I usually define 3 different URLs: a live one (http://example.com), a test one (http://test.example.com) and a local one (http://example) then use a switch (using HTTP_HOST) to check which one I’m on and set the $MODE accordingly.

    Then I can use if for all kinds of things, like whether to include Google Analytics tracking code, how to log/display errors, whether to gzip/minify/chain together my JavaScript and/or CSS, etc.

  3. @deef: I use the $DEBUG variable too.

  4. @Phil: I use this for Google Analytics too!

  5. Jeff Hartman

    I tend to define those as constants instead of variables just so there is no chance of them being overwritten.

  6. yeah i use constants to define set values like database prefixes and email addresses.
    I then use variables to set different changeable values based on a defined constant.
    Like email headers or an applications theme.

    In somecases i have even used a further level of configuration such as xml and mysql. This way i was able to setup and install multiple instances of an application each with their own settings.

  7. codeQuantum

    I do the same thing, but using :

    if ($_SERVER[‘SERVER_ADDR’] == “127.0.0.1”) //home else { //webserver }

    The main advantage is that I can use this same code (I made it into a code snippet) on all my websites. I don’t have to worry about having the right domain name for each case.

  8. visit

    I see we share a common interest! Excellent job on the website!

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!