用过c#的代码判断用户ip是否在指定的ip段内
1
/**/
/// <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 {
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++)
15 {
16 if (inip[i] < begipint[i] || inip[i] > endipint[i])
17 {
18 return false ;
19 }
20 else if (inip[i] > begipint[i] || inip[i] < endipint[i])
21 {
22 return true;
23 }
24 }
25 return true;
26 }
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 {
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++)
15 {
16 if (inip[i] < begipint[i] || inip[i] > endipint[i])
17 {
18 return false ;
19 }
20 else if (inip[i] > begipint[i] || inip[i] < endipint[i])
21 {
22 return true;
23 }
24 }
25 return true;
26 }
其中还会用到函数getIp
1
/**/
/// <summary>
2 /// 将ip地址转成整形数组
3 /// </summary>
4 /// <param name="ip"></param>
5 /// <returns></returns>
6 protected int [] getIp( string ip)
7 {
8 int[] retip = new int[4];
9 int i,count;
10 char c;
11 for (i = 0,count=0; i < ip.Length; i++)
12 {
13 c = ip[i];
14 if (c != '.')
15 {
16 retip[count] = retip[count] * 10 + int.Parse(c.ToString());
17 }
18 else
19 {
20 count++;
21 }
22 }
23
24 return retip;
25
26 }
2 /// 将ip地址转成整形数组
3 /// </summary>
4 /// <param name="ip"></param>
5 /// <returns></returns>
6 protected int [] getIp( string ip)
7 {
8 int[] retip = new int[4];
9 int i,count;
10 char c;
11 for (i = 0,count=0; i < ip.Length; i++)
12 {
13 c = ip[i];
14 if (c != '.')
15 {
16 retip[count] = retip[count] * 10 + int.Parse(c.ToString());
17 }
18 else
19 {
20 count++;
21 }
22 }
23
24 return retip;
25
26 }