Debug Messages, Error Notices, Other Secret Texts and IP Filtering

Sometimes there are instances where you need to debug a live web site without displaying HTTP Status code. Where you can’t create pages where you put notices like “Site Under Maintenance” or those animated GIF’s of a construction worker drilling the road. Those moments where you are itching to parse series of server-side scripts which are in “alien language” for most of your readers. Just an ugly view.

In PHP, there is a way wherein you could still show display those unwanted text for yourself only. Yep! That’s right. Only you and or maybe a few other peeps of your choice while in your readers’ view, they would be entirely invisible. Ooohh, sneaky… 😈

PHP has a very useful reserved variable $_SERVER.

$_SERVER is an array containing information such as headers, paths, and script locations.

It contains an element named REMOTE_ADDR which determines the IP address of the user who is currently viewing a certain page in your site.

This is a simple way to determine your viewing IP address. You could create a test file and add the php code below:

{code type=”php”}
<?php
echo $_SERVER[‘REMOTE_ADDR’];
?>
{/code}

This is the way how those whois sites like ipchicken.com or whatismyip.com determines your ip addresses. Determining one’s ip address helps some sites to ban some users abusing their websites or just simply allow a certain group some access to their. Other programmers checks ip addresses for added security for their websites.

As for this article, we will be using IP filtering to display those nasty debugging messages to ourselves alone.

IP filtering is just a basic method in php that is often overlooked simply because: it’s simple and it’s basic. And maybe, not everyone finds a advantageous use for it.

When I said basic, we would be utilizing these three php fundamentals: if-else statements, array and functions.

Let’s get on the business.

In this article, let us assume that I have checked my ip address already and the value would be “198.198.198.1“.

Filtering a Single IP

Let us say, I want to display a text on a page in my site which would only be visible to me and another set of text for other viewers. We could do it by:

{code type=”php”}
<?php
$accepted_ip = “198.198.198.1”;
if ($_SERVER[‘REMOTE_ADDR’] == $accepted_ip){
echo ‘Hello Self!’;
}else{
echo ‘Hi there stranger’;
}
?>
{/code}

Filtering Multiple IP Addresses

What if I have other friends that I would like to show a secret message. There are two ways to do this.

Approach 1: Test all ip using if-else conditional statements

{code type=”php”}
<?php
if($_SERVER[‘REMOTE_ADDR’] == “200.200.200.1” or $_SERVER[‘REMOTE_ADDR’] == “101.101.101.1” or $_SERVER[‘REMOTE_ADDR’] == “198.198.198.1”){
echo ‘Hello Self and Friends’;
}else{
echo ‘Hello there strangers!’;
}
?>
{/code}

Approach 2: Place all accepted ip’s in an array then do an array search

{code type=”php”}
<?php
$accepted_ip_list[] = “198.198.198.1”;
$accepted_ip_list[] = “200.200.200.1”;
$accepted_ip_list[] = “101.101.101.1”;
//Sorry, not now
//$accepted_ip_list[] = “98.98.98.1”;
if (in_array($_SERVER[‘REMOTE_ADDR’], $accepted_ip_list)){
echo ‘Hello Self and Friends’;
}else{
echo ‘Hello there strangers!’;
}
?>
{/code}

From this two approaches, I like the second better for it is easier to read and easier to manage. In this approach, you could easily comment out an ip that you temporarily want to deny permission to view your page.

Filtering an Range of IP Addresses

What if you are a team working on the same site, in the same office and is within a certain range of ip. Here is how you can do it.

For example, the range of your IP is within 198.198.198.*. The asterisk(*) is a wild card for the numbers between 0-288 which means 198.198.198.10 and 198.198.198.222 is falling within the range of 198.198.198.*.

Quick Info

Our ip addresses is group into four different parts or quadrants by using the dot(.) as the delimiter.

In this situation, we could use strpos built-in php function. In this if-else statement, we will be using “===” to test both the value and type of result returned by strpos.

{code type=”php”}
<?php
if (strpos($_SERVER[‘REMOTE_ADDR’], ‘198.198.198’) === 0){
echo ‘Hi there neighbor’;
}else{
echo ‘Intruder alert!’;
}
?>
{/code}

In the sample code, we dropped the wild card because we only need to check the first three quadrants of our ip which is “198.198.198”

So if we want to check a wider range of ip, we could try:

{code type=”php”}
<?php
if (strpos($_SERVER[‘REMOTE_ADDR’], ‘198.198’) === 0){
echo ‘Hi there neighbor from Second Quadrant’;
}else{
echo ‘Intruder alert!’;
}
?>
{/code}

Or this

{code type=”php”}
<?php
if (strpos($_SERVER[‘REMOTE_ADDR’], ‘198’) === 0){
echo ‘Hi there neighbor from First Quadrant’;
}else{
echo ‘Intruder alert!’;
}
?>
{/code}

Quick info

“strpos” returns the integer position of a string we are looking for. If the string is found in the very beginning of the haystack(the string where we are searching), it will return an “integer” value zero(0) and which means we had found what we are looking for. Else, the function will return FALSE which is a “boolean” that also has an integer value of zero(0) and which means our search failed. Thus, we must test the type of result which is whether it is a boolean or an integer and the value of the position using the equality operator “===”.

Error Messages and Warnings

Now let’s try defining our error reporting messages.

{code type=”php”}
<?php $programmer_ip = ‘198.198.198.1’;
// Display all errors if ($_SERVER[‘REMOTE_ADDR’] == $programmer_ip){
ini_set(‘error_reporting’, E_ALL);
}
// Errors in invisible mode
else{ ini_set(‘error_reporting’, 0);
}
?>
{/code}

Displaying Values PHP Pre-defined Variables

There are times you need to display the value of your submitted form, the contents of your cookies or some server information. These data are delicate that not everyone should be able view.

{code type=”php”}
<?php
$programmer_ip = ‘198.198.198.1’;
if ($_SERVER[‘REMOTE_ADDR’] == $programmer_ip){
var_dump($_SERVER);
print_r($_POST);
}
?>
{/code}

Turning on the Debug Mode in WordPress

WordPress has a default debug switch in its wp-config.php file. NOTE: Backup the file before modifying it.

Inside wp-config.php, look for the line “define(‘WP_DEBUG’, false);” and change it to something like this:

{code type=”php”}
<?php
$programmer_ip = ‘198.198.198.1’;
if ($_SERVER[‘REMOTE_ADDR’] == $programmer_ip){
define(‘WP_DEBUG’, true);
}else{
define(‘WP_DEBUG’, true);
}
?>
{/code}

IP filtering is a very easy method and is quite very handy in some occasions but it is worth learning. Maybe some time in the future, you will have a great use for it.

Thanks for reading.

Recommended Posts