Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Greg Kaleka
Greg Kaleka
39,021 Points

Local / production server check in config.php. Good idea?

As I've migrated my site gregkaleka.com to my production server, the only difference between my local environment version and the live version is my config.php file (included on every page - I use the constants for hrefs, includes, etc). I've already accidentally overwritten the web version and broken the site once.

To solve this problem, I've added some logic to test if the site is local or live before setting the constants. This allows the entire project to be identical locally and on the web. Does anyone see any potential issues I'm not thinking of? Is there a better way to deal with this?

Thanks!

Code below.

<?php

if ($_SERVER["HTTP_HOST"] == "localhost") {
    $web = false;
} else {
    $web = true;
}

if ($web === true) {
    define("BASE_URL", "/");                                                // web
    define("ROOT_PATH", $_SERVER["DOCUMENT_ROOT"] . "/");                   // web
} else {
    define("BASE_URL", "/~greg/gregkaleka_com/");                           // local
    define("ROOT_PATH", $_SERVER["DOCUMENT_ROOT"] . "/gregkaleka_com/");    // local
}

2 Answers

You've got the right idea, but rather than determining your environment via the _SERVER['HTTP_HOST'] variable, I'd suggest you export an environment variable on your production server and check that. The specifics about doing that will vary depends on your web host, operating system, etc. At it's simplest you could do something like this:

> export NATES_WEBSITE_ENV=production

> php -a
Interactive shell

php > echo $_ENV["NATES_WEBSITE_ENV"];
production

It's the same concept as what you're already doing, but better for a couple reasons:

  1. You control the value of the variable you're checking. _SERVER['HTTP_HOST'] may not always be 'localhost' locally.
  2. You're moving the environment out of the application logic.

In general, it's a great idea to move application secrets and configuration bits (like the application environment) to their own environment variables that you set. Most of the applications that make up Treehouse make heavy use of environment variables for configuration stuff just like this.

Edit: PHP's documentation about _ENV: http://php.net/manual/en/reserved.variables.environment.php

Greg Kaleka
Greg Kaleka
39,021 Points

Thanks Nate! I knew this was a good answer, but I didn't actually have any idea what you were talking about haha.

I just finished the Console Foundations lesson on environment variables, and now I can actually implement this! Quick followup question though: I definitely understand advantage 1. that you listed, but I'm not sure how I can move the logic out of the app.

Don't I still have to have all the same logic in my config.php file? Now I'll be checking an environment variable rather than a server variable, but nothing else changes, right?

Thanks again,

Greg

but I'm not sure how I can move the logic out of the app.

Yeah, in your example it wouldn't change much, you still need to check the variable. What I meant was any logic your application may have for setting and managing those configuration bits can technically be considered external to what your application actually does.

So in your projects documentation you can add requirements that the application expects ENV['SOME_VARIABLE'] to be set to determine which environment it's operating in. How you get the variable set doesn't really matter to the application.

Some people manage environment variables by hand, automatically via deployment scripts, or with a configuration management tool like Chef, Puppet or Ancible. I wouldn't sweat this stuff now though, unless you like a steep challenge.

Glad you got the environment variable stuff figured out. It's an extremely common pattern these days (we make heavy use of it at Treehouse).

Here's some really good reading about modern web apps: The Twelve-Factor App. Specifically, #3 is exactly what we're talking about here.

Greg Kaleka
Greg Kaleka
39,021 Points

OK, gotcha. This is making sense to me now. Thanks so much for taking the time to explain all this!

Colin Bell
Colin Bell
29,679 Points

This is a great idea. I usually just add the config file to my FTP ignore list.