How to check if an ip address matches a CIDR

When working in the area of application security, you may come across situations where you need to make sure that only certain servers are allowed to access your web service. Usually checking the ip address is a decent way of making such guarantees.

You can use a function as follows:

function cidr_match($ip, $cidr)
{
    list($subnet, $mask) = explode('/', $cidr);

    if ((ip2long($ip) & ~((1 << (32 - $mask)) - 1) ) == 
ip2long($subnet))
    { 
        return true;
    }

    return false;
}

Example results

cidr_match("1.2.3.4", "0.0.0.0/0"): true
cidr_match("127.0.0.1", "127.0.0.1/32"): true
cidr_match("127.0.0.1", "127.0.0.2/32"): false

Sources:

http://www.php.net/manual/en/function.ip2long.php#82397

http://stackoverflow.com/questions/594112/matching-an-ip-to-a-cidr-mask-in-php5

Setting up and using PHP Memcache

What is Memcache? Memcache is an object-oriented mapping that is used to speed up dynamic database-drive applications by storing data in key-value pairs in RAM. In this blog, I will go through steps involved in setting up Memcache on a machine running Ubuntu and running a simple PHP program with Memcache.

For installing software, I will use Synaptic Package Manager.

Installing Memcache

  • Memcache is a PECL extension , so first, install PEAR. Go to Synaptic, find php-pear, and install it.
  • Next, install PHP development package.  Open Synaptic, find php-dev, and install it.
  • Build memcache module by running the following command:

$ pecl install memcache

  • You will see several lines of text run on the console. When done, you should see a success message and instruction telling you to add a line to your php.ini file. So, add the following line to your php.ini file:

extension=memcache.so

  • Next, we need to install memcached so that we can fire up the memcache process. Open Synaptic Package Manager, and install memcached.
  • Start memcached process by running the following command on a terminal:

$ memcached -d -m 1024 -u root -l 127.0.0.1 -p 11211

  • Now we’re ready to create a php program to test Memcache.

Writing a PHP program using Memcache

Below is a sample program that demonstrates how Memcached works:

$memcache = new Memcache;
$memcache->connect('127.0.0.1', 11211) or 
die("Couldn't connect to memcache");

function getCache($key){
	global $memcache;
	return ($memcache) ? $memcache->get($key) : false;
}

function setCache($key, $object, $timeout = 60){
	global $memcache;
	return ($memcache) ? $memcache->set($key,
 $object, MEMCACHE_COMPRESSED, $timeout) : false;
}

$connection = mysql_connect('localhost', 'root', 'foobar')
 or die("can't connect to mysql");
mysql_select_db('test', $connection);

// test query
$key = md5("SELECT * FROM testtable WHERE id=2");
$get_result = getCache($key);

if($get_result){
	echo $get_result['name'];
	echo $get_result['id'];
	echo "Data pulled from cache";
}else{
	$sql = 	"SELECT * FROM testtable WHERE id=2";
	$result = mysql_query($sql);
	$row = mysql_fetch_array($result);
	print_r($row);
	setCache($key, $row, 20);
	echo "Data pulled from database";
}

It is assumed that the table used exists. If all goes well, you will see that the data is pulled from database initially, and from memcached afterwards.

That’s it for now. Till next time, Goodbye.