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
    Regular Expressions for the Rest of Us

    Sooner or later you'll run across a regular expression. With their cryptic syntax, confusing documentation and massive learning curve, most developers settle for copying and pasting them from StackOverflow and hoping they work. But what if you could decode regular expressions and harness their power? In...

  • By
    Animated 3D Flipping Menu with CSS

    CSS animations aren't just for basic fades or sliding elements anymore -- CSS animations are capable of much more.  I've showed you how you can create an exploding logo (applied with JavaScript, but all animation is CSS), an animated Photo Stack, a sweet...

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
    Duplicate the jQuery Homepage Tooltips

    The jQuery homepage has a pretty suave tooltip-like effect as seen below: The amount of jQuery required to duplicate this effect is next to nothing;  in fact, there's more CSS than there is jQuery code!  Let's explore how we can duplicate jQuery's tooltip effect. The HTML The overall...

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!