WP-Mix

A fresh mix of code snippets and tutorials

PHP run script for specific IP address

When working on a live site (which of course you should never do), it may be useful to execute some PHP script only for your own IP address. This quick tutorial provides a simple script that can make it happen.

$referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : 'undefined';
$agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'undefined';

$address = 'undefined';

if (isset($_SERVER)) {
	if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
		$address = $_SERVER['HTTP_X_FORWARDED_FOR'];
	} elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
		$address = $_SERVER['HTTP_CLIENT_IP'];
	} else {
		$address = $_SERVER['REMOTE_ADDR'];
	}
}

if ($address === '123.123.123.123') {
	
	// do your stuff..
	
}

To flesh this out a bit further, you could replace the IP-detection script to something more robust. Also recommend sanitizing the three variables just to be safe. Otherwise, it’s pretty much ready for drop-in, quick-use testing scenarios (i.e., not meant as a long-term/permanent solution).

Also note that the $referer and $agent variables are not used in the provided example, but they are available should you want to include some additional verification.

★ Pro Tip:

USP ProSAC Pro