Php 重定向IP,如果它是';在一定的IP范围内

Php 重定向IP,如果它是';在一定的IP范围内,php,Php,如果用户的ip在某个ip范围内,我将使用重定向。但是,我使用多个ip范围,所以我想知道最好的方法。我正在用这个来重定向 但是如果IP范围是72.122.166.0-72.122.159.266和68.61.156.0-68.61.181.255和78.121.74.0-78.121.77.255,那么我该怎么做呢?谢谢 检查IP范围的最佳方法是将虚线地址转换为32位数字,并对其进行比较。该函数可以为您进行转换。例如: $range_start = ip2long("68.61.156.0");

如果用户的ip在某个ip范围内,我将使用重定向。但是,我使用多个ip范围,所以我想知道最好的方法。我正在用这个来重定向


但是如果IP范围是72.122.166.0-72.122.159.266和68.61.156.0-68.61.181.255和78.121.74.0-78.121.77.255,那么我该怎么做呢?谢谢

检查IP范围的最佳方法是将虚线地址转换为32位数字,并对其进行比较。该函数可以为您进行转换。例如:

$range_start = ip2long("68.61.156.0");
$range_end   = ip2long("68.61.181.255");
$ip          = ip2long($_SERVER['REMOTE_ADDR']);
if ($ip >= $range_start && $ip <= $range_end) {
  // blocked
}
$range_start=ip2long(“68.61.156.0”);
$range_end=ip2long(“68.61.181.255”);
$ip=ip2long($_服务器['REMOTE_ADDR']);

如果($ip>=$range\u start&&$ip如果您愿意使用SQL,并且有一个ip范围表

SELECT * FROM `ips` WHERE $ip BETWEEN `start` AND `end`
如果结果为零,则不会被阻止

编辑:当然,使用
ip2long
功能


如果你有很多随机范围,这是一种更好的方法;纯PHP方法更适合更少的范围。

我希望你不要用它来阻止被IP禁止的用户访问你的页面,因为这对于像现在这样动态的IP来说是非常无用的。另外,请注意,
72.122.159.266
72.122.166.0
都是无效的IP地址,而
68.61.181.255
78.121.77.255
都是广播地址!请用示例检查我的地址。看,执行no?Th可能会很慢nk建议扩展您的答案,而不仅仅是粘贴在代码中(尽管带有注释)。
<?php
/* VARIABLES */
// -------------------------------
$ip = ip2long($_SERVER['REMOTE_ADDR']);
$current_ip_range = array(); // Class "X" range.

$range = (object) array();
//$range->name = '24-Bit Block';
$range->lower = ip2long('0.0.0.0'); //This is the initial value.
$range->upper = ip2long('83.50.207.254'); //This is the final value.
$current_ip_range[] = $range;
// -------------------------------

if ($ip >= $range->lower && $ip <= $range->upper) 
{
        /* ----------------------------------------
         * We ask to the server if the IP grabbed
         * is <= or => to the ip ranges and if if does
         * so, shows success message.
         * -------------------------------------- */
        echo "La IP $ip está dentro del rango";

}

else
{
        /* ----------------------------------------
         * If it isn't, shows a failure message.
         * -------------------------------------- */
        echo "La ip $ip no está dentro del rango";

}