<?php
/*
__ _ ___ ___ ___ ___ ___ ____ _ __ ___ ___
/ _` |/ / / __/ _ \ / _ \ / / / __/| '_ ` _ \ / /
| (_| |\ \| (_| (_) | (_) |\ \ | (__ | | | | | |\ \
\__,_|/__/ \___\___/ \___/ /__/ \___\|_| |_| |_|/__/
*/
/*
* FUNCTION : isPrivateIP($ip)
* DESCRIPTION : Check if an IP is local, personal and not public address.
* PARAMS
* $ip (String) : The web address you want to consider.
* RETURN : FALSE if it does not belong to the list of registered address, otherwise if there returns TRUE
*
* ASCOOS CMS USE : $private = $objNetwork->isPrivateIP($ip);
* PUBLIC USE :
* ---------------
* $ip = "172.27.38.110";
* $private = isPrivateIP($ip);
*
* if ($private) {
* echo "The $ip is a private network address\n";
* } else if ($private === false) {
* echo "The $ip is NOT a private network address\n";
* } else {
* echo "The $ip is NOT a valid IP address\n";
* }
*
*/
function isPrivateIP($ip) {
if (empty($ip) or !ip2long($ip)) return NULL;
$private_ips = array (
array('10.0.0.0','10.255.255.255'),
array('172.16.0.0','172.31.255.255'),
array('192.168.0.0','192.168.255.255')
);
$ip = ip2long($ip);
foreach ($private_ips as $ipr) {
$min = ip2long($ipr[0]);
$max = ip2long($ipr[1]);
if (($ip >= $min) && ($ip <= $max)) return true;
}
return false;
}
/***************
* EXAMPLE
***************/
$ip = "172.27.38.110";
$private = isPrivateIP($ip);
if ($private) {
echo "The $ip is a private network address\n";
} else if ($private === false) {
echo "The $ip is NOT a private network address\n";
} else {
echo "The $ip is NOT a valid IP address\n";
}
?>
This simple function of the Ascoos Cms, check if an IP is local, personal and not public address. Can be used and outside of the Ascoos Cms.
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.