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
    LightFace:  Facebook Lightbox for MooTools

    One of the web components I've always loved has been Facebook's modal dialog.  This "lightbox" isn't like others:  no dark overlay, no obnoxious animating to size, and it doesn't try to do "too much."  With Facebook's dialog in mind, I've created LightFace:  a Facebook lightbox...

  • By
    Vibration API

    Many of the new APIs provided to us by browser vendors are more targeted toward the mobile user than the desktop user.  One of those simple APIs the Vibration API.  The Vibration API allows developers to direct the device, using JavaScript, to vibrate in...

Incredible Demos

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!