Re: Getting totals
| From: | Jochem Maas | Date: | Tue, 06 Jun 2006 18:03:45 +0000 |
| Subject: | Re: Getting totals | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-237476@lists.php.net to get a copy of this message | ||
without actually thinking about your problem I guess that these funcs
(the first is most like useful, the second show [at least] how the first
can be used and the last func is just for fun) might help you somehow
(apologies if If I'm way off base):
ALSO: if anyone has a cleaner/faster/better way of doing this
then I'm intersted to learn :-)
/* Determine if an ip is in a net.
* E.G. 120.120.120.120 in 120.120.0.0/16
*/
function isIPInSubnet($ip, $net, $mask)
{
$firstpart = substr(str_pad(decbin(ip2long($net)), 32, "0", STR_PAD_LEFT) ,0 ,
$mask);
$firstip = substr(str_pad(decbin(ip2long($ip)), 32, "0", STR_PAD_LEFT), 0, $mask);
return (strcmp($firstpart, $firstip) == 0);
}
/* This function check if a ip is in an array of nets (ip and mask) */
function isPrivateIP($theip)
{
foreach (array("10.0.0.0/8",
"172.16.0.0/12",
"192.168.0.0/16") as $subnet)
{
list($net, $mask) = explode('/', $subnet);
if(isIPInSubnet($theip,$net,$mask)) {
return true;
}
}
return false;
}
/* Building the ip array with the HTTP_X_FORWARDED_FOR and REMOTE_ADDR HTTP vars.
* With this function we get an array where first are the ip's listed in
* HTTP_X_FORWARDED_FOR and the last ip is the REMOTE_ADDR
*/
function getRequestIPs()
{
$ipList = array();
foreach (array('HTTP_X_FORWARDED_FOR', 'HTTP_FORWARDED_FOR',
'REMOTE_ADDR') as $key) {
if (isset($_SERVER[$key]) && $_SERVER[$key]) {
$ipList = array_merge($ipList, explode(',', $_SERVER[$key]));
break;
}
}
return $ipList;
}
Rob W. wrote:
> Ok, Here is my next problem.
>
> Inside my database, I have a list of ip's of about 10 blocks
>
> 192.168.100.0 all the way though 255
> along with
> 192.168.101.0 though 255
> and
> 192.168.102.0 though 255
> and soforth
>
> My problem is, is i'm trying to figure out a pattern to match so I can count how many
> ip's are in each block and display them.
>
> So far what I have gotten is a stristr match but it's not working correctly. I have a
> variable that basically weed's out the last digits of the ip it's self from help
> previously
>
> So my code so far is:
>
> if (stristr($block,$address)) {
> $count_ip++;
> }
>
> $block would == 192.168.100
> $address would == 192.168.100.0 - 255
>
> Any help would be appricated.