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
    Responsive Images: The Ultimate Guide

    Chances are that any Web designers using our Ghostlab browser testing app, which allows seamless testing across all devices simultaneously, will have worked with responsive design in some shape or form. And as today's websites and devices become ever more varied, a plethora of responsive images...

  • By
    Create a CSS Flipping Animation

    CSS animations are a lot of fun; the beauty of them is that through many simple properties, you can create anything from an elegant fade in to a WTF-Pixar-would-be-proud effect. One CSS effect somewhere in between is the CSS flip effect, whereby there's...

Incredible Demos

  • By
    AJAX For Evil:  Spyjax with jQuery

    Last year I wrote a popular post titled AJAX For Evil: Spyjax when I described a technique called "Spyjax": Spyjax, as I know it, is taking information from the user's computer for your own use — specifically their browsing habits. By using CSS and JavaScript, I...

  • By
    Page Visibility API

    One event that's always been lacking within the document is a signal for when the user is looking at a given tab, or another tab. When does the user switch off our site to look at something else? When do they come back?

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!