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
    Create Namespaced Classes with MooTools

    MooTools has always gotten a bit of grief for not inherently using and standardizing namespaced-based JavaScript classes like the Dojo Toolkit does.  Many developers create their classes as globals which is generally frowned up.  I mostly disagree with that stance, but each to their own.  In any event...

  • 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
    Six Degrees of Kevin Bacon Using MooTools 1.2

    As you can probably tell, I try to mix some fun in with my MooTools madness but I also try to make my examples as practical as possible. Well...this may not be one of those times. I love movies and useless movie trivia so naturally I'm...

  • By
    Facebook Open Graph META Tags

    It's no secret that Facebook has become a major traffic driver for all types of websites.  Nowadays even large corporations steer consumers toward their Facebook pages instead of the corporate websites directly.  And of course there are Facebook "Like" and "Recommend" widgets on every website.  One...

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!