基于IP范围的限制

发布于 2024-11-10 18:42:46 字数 250 浏览 3 评论 0原文

我正在构建一个管理面板。我想阻止某些 IP 范围。我正在我的本地主机 wamp 服务器上测试这个,但 ir 似乎没有重定向我。

<?php
   if($_SERVER['REMOTE_ADDR'] == '127.0.0..*') 
      header("Location: http://google.com");
   else
      echo "Hai";
?>

任何意见都会受到赞赏。

I am building an admin panel. and I want to block certain IP ranges. I'm testing this on my localhost wamp server but ir doesn't seem to redirect me.

<?php
   if($_SERVER['REMOTE_ADDR'] == '127.0.0..*') 
      header("Location: http://google.com");
   else
      echo "Hai";
?>

Any input is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

心凉 2024-11-17 18:42:46

使用字符串比较就足够了

if (strncmp('127.0.0.', $_SERVER['REMOTE_ADDR'], 8) === 0)
  header("Location: http://google.com");
else
  echo "Hai";

更新:取自 inits 答案的评论

假设我想阻止来自此范围的任何 IP:192.168.0.1-255。最好的解决方案是什么?谢谢。

然后只需对该块进行相同的字符串比较

if (strncmp('192.168.0.', $_SERVER['REMOTE_ADDR'], 10) === 0)
  header("Location: http://google.com");
else
  echo "Hai";

如果您想同时针对两个块测试远程地址,您可能会将它们放在一个表达式中。这次我们需要一种不同的方法

if (in_array(substr($_SERVER['REMOTE_ADDR'], 0, strrpos($_SERVER['REMOTE_ADDR'], '.')), array('127.0.0', '192.168.0'))) {
  header("Location: http://google.com");
else
  echo "Hai";

substr() 部分获取 IP,直到最后一个 .。我们可以尝试在 IP 前缀集合(-> 数组)中找到该字符串。

Is sufficient to use string comparison

if (strncmp('127.0.0.', $_SERVER['REMOTE_ADDR'], 8) === 0)
  header("Location: http://google.com");
else
  echo "Hai";

Update: Taken from the comments of inits answer

Suppose i want to block any IP coming from this range: 192.168.0.1-255. What would be the best solution for it ? Thanks.

Then just make the same string comparison against this block

if (strncmp('192.168.0.', $_SERVER['REMOTE_ADDR'], 10) === 0)
  header("Location: http://google.com");
else
  echo "Hai";

If you want to test the remote address against both blocks at once, you will probably put them together into one expression. This time we need a different approach

if (in_array(substr($_SERVER['REMOTE_ADDR'], 0, strrpos($_SERVER['REMOTE_ADDR'], '.')), array('127.0.0', '192.168.0'))) {
  header("Location: http://google.com");
else
  echo "Hai";

The substr()-part takes the IP until the last .. We can just try to find this string in a set (-> array) of IP-prefixes.

等风也等你 2024-11-17 18:42:46
$ip0 = ip2long("127.0.0.1");
$ip1 = ip2long("127.0.0.254");
$ip  = ip2long($_SERVER['REMOTE_ADDR']);

if ($ip0 <= $ip && $ip <= $ip1) {
    echo long2ip($ip) . " is inside  range " . long2ip($ip0) . "-" . long2ip($ip1);
}
else {
    echo long2ip($ip) . " is outside range " . long2ip($ip0) . "-" . long2ip($ip1);
}
$ip0 = ip2long("127.0.0.1");
$ip1 = ip2long("127.0.0.254");
$ip  = ip2long($_SERVER['REMOTE_ADDR']);

if ($ip0 <= $ip && $ip <= $ip1) {
    echo long2ip($ip) . " is inside  range " . long2ip($ip0) . "-" . long2ip($ip1);
}
else {
    echo long2ip($ip) . " is outside range " . long2ip($ip0) . "-" . long2ip($ip1);
}
浊酒尽余欢 2024-11-17 18:42:46

这将是一个更好的方法,使用正则表达式:

// returns true for IPs 127.0.0.0-255
if (preg_match("'^127[.]0[.]0[.][0-9]+'",$_SERVER['REMOTE_ADDR']))
{
    header("Location: http://google.com");
}
else
{
    echo "Hai";
}

编辑: 很好,将其提升到另一个级别,也许不是最有效的,但更容易配置:

$mask = "192.168.1.1-255";

$ip = explode(".",$_SERVER['REMOTE_ADDR']);
$in = 0;
foreach (explode(".",$mask) as $k => $v)
{
    if (preg_match("'^([0-9]+)-([0-9]+)
",$v,$n))
    {
        if ($ip[$k] >= $n[1] && $ip[$k] <= $n[2]) $in++;
    }
    elseif (preg_match("'^[0-9]+
",$v,$n))
    {
        if ($ip[$k] == $n[0]) $in++;
    }
}
if ($in == 4)
{
    header("Location: http://google.com");
}
else
{
    echo "Hai";
}

This would be a better approach, using regualr expression:

// returns true for IPs 127.0.0.0-255
if (preg_match("'^127[.]0[.]0[.][0-9]+'",$_SERVER['REMOTE_ADDR']))
{
    header("Location: http://google.com");
}
else
{
    echo "Hai";
}

EDIT: Fine, take it to another level, maybe not the most effective, but easier to configure:

$mask = "192.168.1.1-255";

$ip = explode(".",$_SERVER['REMOTE_ADDR']);
$in = 0;
foreach (explode(".",$mask) as $k => $v)
{
    if (preg_match("'^([0-9]+)-([0-9]+)
",$v,$n))
    {
        if ($ip[$k] >= $n[1] && $ip[$k] <= $n[2]) $in++;
    }
    elseif (preg_match("'^[0-9]+
",$v,$n))
    {
        if ($ip[$k] == $n[0]) $in++;
    }
}
if ($in == 4)
{
    header("Location: http://google.com");
}
else
{
    echo "Hai";
}
So要识趣 2024-11-17 18:42:46

以下是我针对仅允许某些固定 IP 和某些 IP 范围问题的解决方案:

$ClientIP = $_SERVER['REMOTE_ADDR'];
$First3PartsOfIP = substr($ClientIP, 0, strrpos($ClientIP, '.'));
$AllowedIPs = ['127.0.0.1'];
$AllowedFirst3Parts = ['172.20.8', '172.21.13'];
if (!in_array($ClientIP, $AllowedIPs) && !in_array($First3PartsOfIP, $AllowedFirst3Parts)) {
    //echo "Your IP: $ClientIP<br />";
    die("Access Denied!");
}

Here is my solution to the problem of just allowing some fixed IPs and some IP ranges:

$ClientIP = $_SERVER['REMOTE_ADDR'];
$First3PartsOfIP = substr($ClientIP, 0, strrpos($ClientIP, '.'));
$AllowedIPs = ['127.0.0.1'];
$AllowedFirst3Parts = ['172.20.8', '172.21.13'];
if (!in_array($ClientIP, $AllowedIPs) && !in_array($First3PartsOfIP, $AllowedFirst3Parts)) {
    //echo "Your IP: $ClientIP<br />";
    die("Access Denied!");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文