判断用户ip是否在指定的ip段内

用过c#的代码判断用户ip是否在指定的ip段内

 

 1 ExpandedBlockStart.gif ContractedBlock.gif          /**/ /// <summary>
 2        /// 检测用户ip是否在指定的ip段中
 3        /// </summary>
 4        /// <param name="ip">用户ip</param>
 5        /// <param name="begip">起始ip</param>
 6        /// <param name="endip">结束ip</param>
 7        /// <returns></returns>

 8          protected   bool  isinip( string  ip,  string  begip,  string  endip)
 9 ExpandedBlockStart.gifContractedBlock.gif         {
10            int[] inip, begipint, endipint = new int[4];
11            inip = getIp(ip);
12            begipint = getIp(begip);
13            endipint = getIp(endip);
14            for (int i = 0; i < 4; i++)
15ExpandedSubBlockStart.gifContractedSubBlock.gif            {
16                if (inip[i] < begipint[i] || inip[i] > endipint[i])
17ExpandedSubBlockStart.gifContractedSubBlock.gif                {
18                    return false ;
19                }

20                else if (inip[i] > begipint[i] || inip[i] < endipint[i])
21ExpandedSubBlockStart.gifContractedSubBlock.gif                {
22                    return true;
23                }

24            }

25            return true;
26        }

 

 

其中还会用到函数getIp

 

 1 ExpandedBlockStart.gif ContractedBlock.gif          /**/ /// <summary>
 2        /// 将ip地址转成整形数组
 3        /// </summary>
 4        /// <param name="ip"></param>
 5        /// <returns></returns>

 6          protected   int [] getIp( string  ip)
 7 ExpandedBlockStart.gifContractedBlock.gif         {
 8            int[] retip = new int[4];
 9            int i,count;
10            char c;
11            for (i = 0,count=0; i < ip.Length; i++)
12ExpandedSubBlockStart.gifContractedSubBlock.gif            {
13                c = ip[i];
14                if (c != '.')
15ExpandedSubBlockStart.gifContractedSubBlock.gif                {
16                    retip[count] = retip[count] * 10 + int.Parse(c.ToString());
17                }

18                else
19ExpandedSubBlockStart.gifContractedSubBlock.gif                {
20                    count++;
21                }

22            }

23
24            return retip;
25            
26        }

转载于:https://www.cnblogs.com/lts8989/archive/2009/02/09/1386697.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
判断IP地址是否指定地区范围内,可以使用IP地址库和Java中的IP地址操作类。以下是一个简单的示例: ```java import java.net.InetAddress; import java.net.UnknownHostException; // 使用IP地址库来获取IP地址所属的地区 import com.maxmind.geoip2.DatabaseReader; import com.maxmind.geoip2.exception.GeoIp2Exception; import com.maxmind.geoip2.model.CityResponse; import com.maxmind.geoip2.record.Country; import com.maxmind.geoip2.record.Subdivision; public class IpRegionChecker { // GeoLite2-City.mmdb为IP地址库文件,可以从MaxMind官网下载 private static final String IP_DATABASE_PATH = "GeoLite2-City.mmdb"; private DatabaseReader reader; public IpRegionChecker() throws Exception { reader = new DatabaseReader.Builder(getClass().getResourceAsStream(IP_DATABASE_PATH)).build(); } public String getRegion(String ipAddress) throws UnknownHostException, GeoIp2Exception { InetAddress inetAddress = InetAddress.getByName(ipAddress); CityResponse response = reader.city(inetAddress); Country country = response.getCountry(); Subdivision subdivision = response.getMostSpecificSubdivision(); String region = subdivision.getName(); return region; } public static void main(String[] args) throws Exception { IpRegionChecker checker = new IpRegionChecker(); String ipAddress = "192.168.1.100"; // 要判断IP地址 String region = checker.getRegion(ipAddress); // 获取该IP地址所属的地区 System.out.println("IP地址 " + ipAddress + " 所属地区为:" + region); } } ``` 需要使用MaxMind提供的IP地址库,可以从其官网下载。该示例使用了GeoLite2-City.mmdb文件,可以在构造函数中指定IP地址库文件路径。在`getRegion()`方法中,首先使用InetAddress类将IP地址转换为InetAddress对象,然后使用DatabaseReader类读取IP地址库文件,获取该IP地址所属的国家和地区信息。最后返回该IP地址所属的地区名称。可以根据需要修改该示例来满足不同的需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值