本文介绍了查找给定的IP是否存在于CIDR中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到使用$ _SERVER ['REMOTE_ADDR']的用户的IP地址,该用户试图访问我的网站。我有不同的盒子与不同的IDS和不同的CIDR条目在我的数据库中的每个框。我想检查用户IP是否存在于任何CIDR或特定框中。
如果存在,则允许他输入否则不输入。
假设用户IP是

  127.0.0.1 
pre>

假设框12的CIDR条目是

  192.168。 1.20 / 27 
192.168.0.10/32

CIDR条目存储在数据库



如果他带有这个范围内的IP,那么他应该被允许进入该网站,否则不允许。
我想检查他的ip对每个CIDR条目。



有任何想法吗?



解决方案

确定,请尝试以下5个简单步骤...



1。将CIDR存储到数组中(从数据库读取,猜测您知道如何获取)

  $ cidrs = array(
'192.168.1.20/27',
'192.168.0.10/32'
);



2。获取用户的IP(远程地址)

  $ user_ip = $ _SERVER ['REMOTE_ADDR']; 



3。添加此功能

 函数IPvsCIDR($ user_ip,$ cidr){
$ parts = explode('/',$ cidr);
$ ipc = explode('。',$ parts [0]);
foreach($ ipc as& $ v)
$ v = str_pad(decbin($ v),8,'0',STR_PAD_LEFT);
$ ipc = substr(join('',$ ipc),0,$ parts [1]);
$ ipu = explode('。',$ user_ip);
foreach($ ipu as& $ v)
$ v = str_pad(decbin($ v),8,'0',STR_PAD_LEFT);
$ ipu = substr(join('',$ ipu),0,$ parts [1]);
return $ ipu == $ ipc;
}



4。比较用户的IP地址与 $ cidrs

  $ validaddr = false; 
foreach($ cidrs as $ addr)
if(IPvsCIDR($ user_ip,$ addr)){
$ validaddr = true;
break;
}

5。决定如何处理用户

  if($ validaddr){
echoCORRECT IP ADDRESS;
}
else {
echoINCORRECT IP ADDRESS;
}

就是这样!



这个函数如何工作。它将CIDR地址部分(x.x.x.x)转换为二进制字符串,并取前N个数字。



示例2(从函数完成作业)



  function testUserIP($ user_ip,$ cidrs){
$ ipu = explode('。',$ user_ip);
foreach($ ipu as& $ v)
$ v = str_pad(decbin($ v),8,'0',STR_PAD_LEFT);
$ ipu = join('',$ ipu);
$ res = false;
foreach($ cidrs as $ cidr){
$ parts = explode('/',$ cidr);
$ ipc = explode('。',$ parts [0]);
foreach($ ipc as& $ v)$ v = str_pad(decbin($ v),8,'0',STR_PAD_LEFT);
$ ipc = substr(join('',$ ipc),0,$ parts [1]);
$ ipux = substr($ ipu,0,$ parts [1]);
$ res =($ ipc === $ ipux);
if($ res)break;
}
return $ res;
}



用法:



  $ user_ip = $ _SERVER ['REMOTE_ADDR']; 
$ cidrs = array('192.168.1.20/27','192.168.0.10/32');
if(testUserIP($ user_ip,$ cidrs)){
//用户ip是ok
}
else {
//访问被拒绝
}


I am getting the IP address of a user using $_SERVER['REMOTE_ADDR'] who tries to visit my site. I have different boxes with different IDS and different CIDR entries in my database for each box. I want to check if the user IP exists in any of the CIDR or not for a specific box.If it exists then he is allowed to enter otherwise not.let suppose the user IP is

127.0.0.1

suppose The CIDR entries for box 12 is

192.168.1.20/27
192.168.0.10/32

The CIDR entries are stored in a column in database

if he come with an IP that comes inside this range then he should be allowed to enter the site otherwise not.I want to check his ip against each CIDR entry.

Any ideas please?

Thanks

解决方案

OK, try following this 5 simple steps...

1. Store your CIDRs into array (read 'em from database; guess you know how to get this)

$cidrs = array(
  '192.168.1.20/27',
  '192.168.0.10/32'
  );

2. Get user's IP (remote address)

$user_ip = $_SERVER['REMOTE_ADDR'];

3. Add this function

function IPvsCIDR($user_ip, $cidr) {
  $parts = explode('/', $cidr);
  $ipc = explode('.', $parts[0]);
  foreach ($ipc as &$v)
    $v = str_pad(decbin($v), 8, '0', STR_PAD_LEFT);
  $ipc = substr(join('', $ipc), 0, $parts[1]);
  $ipu = explode('.', $user_ip);
  foreach ($ipu as &$v)
    $v = str_pad(decbin($v), 8, '0', STR_PAD_LEFT);
  $ipu = substr(join('', $ipu), 0, $parts[1]);
  return $ipu == $ipc;
  }

4. Compare user's IP address against $cidrs

$validaddr = false;
foreach ($cidrs as $addr)
  if (IPvsCIDR($user_ip, $addr)) {
    $validaddr = true;
    break;
    }

5. Decide what to do with user

if ($validaddr) {
  echo "CORRECT IP ADDRESS";
  }
else {
  echo "INCORRECT IP ADDRESS";
  }

That's it!

how this function works. It converts CIDR address-part (x.x.x.x) into binary string and takes first N digits. Then it does same job with user's IP and checks do values match.

Example 2 (complete job from function)

function testUserIP($user_ip, $cidrs) {
  $ipu = explode('.', $user_ip);
  foreach ($ipu as &$v)
    $v = str_pad(decbin($v), 8, '0', STR_PAD_LEFT);
  $ipu = join('', $ipu);
  $res = false;
  foreach ($cidrs as $cidr) {
    $parts = explode('/', $cidr);
    $ipc = explode('.', $parts[0]);
    foreach ($ipc as &$v) $v = str_pad(decbin($v), 8, '0', STR_PAD_LEFT);
    $ipc = substr(join('', $ipc), 0, $parts[1]);
    $ipux = substr($ipu, 0, $parts[1]);
    $res = ($ipc === $ipux);
    if ($res) break;
    }
  return $res;
  }

Usage:

$user_ip = $_SERVER['REMOTE_ADDR'];
$cidrs = array('192.168.1.20/27', '192.168.0.10/32');
if (testUserIP($user_ip, $cidrs)) {
  // user ip is ok
  }
else {
  // access denied
  }

这篇关于查找给定的IP是否存在于CIDR中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 05:58