Deactivate Code Automatically on a Test Server

When fixing a bug or adding a new feature to a website, it may be beneficial to test the changes on a development server before going live. The problem is that some aspects of the code probably shouldn't be active on the development server. For example, we probably don't Google Analytics tracking any of those visits. The code could be manually disabled during the test phase, but are we going to remember? Instead let's look into automating the process.

To check which server is being used to view our page being updated, we'll utilize the strpos() function and the $_SERVER['DOCUMENT_ROOT'] variable. If the development server is accessed using, we can do something like the following:

if(strpos($_SERVER['DOCUMENT_ROOT'], '') !== false) {
     print 'Development server';
} else {
     print 'Live server';

To get the Google Analytics code to only work on our live website, the code would be placed in the else portion. Since we don't need both the if and else portion, the code could be rewritten as follows:

if(strpos($_SERVER['DOCUMENT_ROOT'], '') === false) {
     //Google Analytics code goes here


With the new code in place, we no longer need to worry about disabling aspects of our code before testing on the development server. It may not be complicated to disable the code manually, but it does require us to do it every time. Mistakes happen; especially when it comes to simple tasks. So let PHP handle the tasks for us.


There are currently no comments.

Leave a Comment