Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
burger menu icon
WillMaster

WillMaster > LibraryWebsite Development and Maintenance

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Content for Specific IP Address

PHP web pages can contain content to be published only for certain specified IP addresses. Or, content can be omitted for specific IP addresses.

There are a few reasons a person would want to do that. When the functionality is needed, it is important to have it available.

  1. On a live web page, incremental changes may need to be seen by the site owner or designer but not by anybody else.

  2. When a site owner desires to show a live web page to a friend, or a designer to a client, notes about certain sections of the web page can be published for the friend or client's IP address and omitted from all others.

  3. During development, when there is a login to keep out search engine spiders and inquisitive surf-by folks, the login credentials can be published on the page with PHP for only the site owner and developer's IP addresses. The login credentials will not be anywhere in the source code after the browser or robot loads the web page (see When and Where PHP Code Runs) — except for the specified IP addresses.

The way it works is that the IP address or addresses to consider are custom included in the PHP code.

A PHP variable named $IsIPmatch is consulted to determine if certain content should or should not be published.

The value of $IsIPmatch is determined by consulting the IP address of the browser/robot that requested the web page. If the IP address matches an IP address you specified, the variable $IsIPmatch is assigned the value of true. Otherwise, it is assigned the value of false.

When assigned a true/false value, $IsIPmatch can used within the web page to publish or not publish certain content.

Assigning a Value to the $IsIPmatch Variable

The variable $IsIPmatch has a value assigned once on every web page that will use it. The value is either true or false, depending on whether an IP address match was made.

The code to assign the value needs to be somewhere on the page above the first place where $IsIPmatch will be used.

Here is the code. Following the code is information about the one place to customize.

<?php
$IsIPmatch = false;
TestIPaddresses($IsIPmatch);
function TestIPaddresses(&$IsIPmatch)
{
   // Between the two lines that contain the word IPLIST somewhere within them, 
   //    specify the IP addresses to check. Separate IP addresses with one or 
   //    more white space characters (line-feeds, spaces, blank lines, tabs)
   $IPaddresses = <<<IPLIST
123.456.789.98
4.5.6.73
IPLIST;
   foreach( preg_split('/\s+/',trim($IPaddresses)) as $ip )
   {
      if( $_SERVER['REMOTE_ADDR'] == $ip )
      {
         $IsIPmatch = true;
         break;
      }
   }
}
?>

The one place to customize is between the
$IPaddresses = <<<IPLIST
line and the
IPLIST;
line.

Leave those two blue colored lines as they are. But between those two lines, specify the IP address or IP addresses that may be matched.

When you specify more than one IP address, separate the IP addresses with any white space characters. You may use one or more line-feed characters, spaces, and/or tabs to separate IP addresses.

As indicated earlier, the above code needs to be on every web page that uses the $IsIPmatch variable — somewhere above where $IsIPmatch will first be used.

Using the $IsIPmatch Variable

The variable $IsIPmatch can now be used within the web page to publish or not publish certain content depending on the $IsIPmatch value.

To publish content when there is a match, do it this way.

<?php if($IsIPmatch): ?>
[Content to publish 
for the matched IP address.]
<?php endif; ?>

The above code can be used on a web page as many times as needed.

The content may be any valid web page content.

For the opposite of the above, to publish content when there is no match, do it this way.

<?php if(!$IsIPmatch): ?>
[Content to publish 
when no IP address matched.]
<?php endif; ?>

The difference in the PHP code above is the exclamation mark immediately in front of the variable name. The !$IsIPmatch means "not $IsIPmatch" or, another way to look at it, "$IsIPmatch is false".

As before, the code can be used on a web page as many times as needed. And, the content may be any valid web page content.

Using It

Specifying certain content on a web page for specific IP addresses, or for non-specified IP addresses, is an easily implemented technique.

Without this technique, a separate web page could be created for specific IP addresses only. But the technique is easy and quickly implemented.

(This article first appeared with an issue of the Possibilities newsletter.)

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC