having fun with code

Adding specific code for production only

When you are developing a website you want to get as close as production as possible, to guarantee the final result will be appropriate and to make sure everything is going to work fine. But there are special cases when you want to add some code only on production, like stats code for example.

One easy way to do this is to check the server HTTP variables, the HTTP_HOST to be exact. This will contain your server host name, without the “http://” and the page URL. In my case, the value is “dev.enekoalonso.com”.

In PHP:

  1. <?php if ($_SERVER['HTTP_HOST'] == 'dev.enekoalonso.com'): ?>
  2. [Google Analytics code here]
  3. <?php endif; ?>

That way I don’t mess up my stats while spending hours creating a demo in my development environment.

Related Posts:

2 Comments to Adding specific code for production only

  1. December 29, 2008 at 03:39 | Permalink

    I think the solution depends on the programmer, and there could be as many solutions as programmers out there ;)

    I like to use a variable (e.g: “IS_ALIVE”, “IS_PRODUCTION”, …) in the configuration file that will be TRUE after deploying the web app.

    The only advantage I see for this method is that you have to write less if your page has different parts that are only enabled in production, but the usual case is just enabling analytics, so is not a great improvement anyway ;)

Leave a Reply

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Additional comments powered by BackType

About the blog

This is a blog about development, focused mainly on Javascript but also other languages like python, shell scripts and more.

About the author

Eneko Alonso is a software engineer and UI developer with more than eight years of experience in software and web development. He lives in San Luis Obispo, California and works at LEVEL Studios.

Contact Info

Contact Info

PromoteJS

JavaScript JS Documentation