Development, General, Websites

Testing offline websites? How to cut the online widgets

April 15th, 2008

This tip is for web developers that have a server set up with PHP and MySQL. This setup is required to test many dynamic websites, including blogging systems such as Wordpress.

In your website, you may have any myriad of add-on scripts and widgets that rely on an internet connection to work. If you have affiliate banner ads, use Google Analytics, or use a link exchange service like Entrecard, then you will need a persistent internet connection for them to work well.

This could be an issue if you don’t always have a guaranteed connection. But even when broadband is commonplace almost everywhere, you don’t want online tools to interfere with your local development process. They may break a layout, load erratically, or they might cause some unexpected behavior. For instance, if you’re pulling in the same stat tracking code from two different locations.

What I used to do before to solve this problem was to make an offline copy of the page without these scripts. But it’s repetitive going back to save it under the original name and paste your scripts again when you’re going to upload the page. The better solution is elegant and simple. Instead you need just the original page, and insert this small piece of code wherever you need to use scripts that need to access the interwebs. Simply add the scripts inside the block:

<?php if (@$_SERVER['REMOTE_ADDR'] != "127.0.0.1") { ?>
<!-- Add your various online scripts here -->
<?php } ?>

This would display whatever’s between the curly braces, only if the remote IP address is NOT 127.0.0.1. Escaping the PHP code would then be able to display any HTML or Javascript. Since you would only get this address if you’re running it on your own server, your online scripts won’t run locally. Otherwise, it will usually get the IP address that’s given by your internet service provider, and your scripts will run.

Leave a Reply