对用户IP进行访问限制

 1 //ashx文件 
 2 
 3 public void ProcessRequest (HttpContext context) {
 4 
 5                      string action=context.Request["action"];
 6 
 7 if (action == "GetIP")
 8 {
 9 string result;
10 result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
11 if (null == result || result == String.Empty)
12 {
13 result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
14 }
15 
16 if (null == result || result == String.Empty)
17 {
18 result = HttpContext.Current.Request.UserHostAddress;
19 }
20 IP ip = IPManager.GetOne("select * from IP where '" + result + "' between IP_Start and IP_End");
21 if (ip.ID > 0)
22 {
23 context.Response.Write(1);
24 }
25 else
26 {
27 context.Response.Write(0);
28 } //取得数据后返回给客户端。
29 
30 }
31 }
32 //html
33 
34 <script type="text/javascript">
35 $(function getIP() {
36 $.post("/ashx/PageNews.ashx", { "action": "GetIP" }, function (data) {
37 var json = eval(data);
38 if (json == "1") {
39 document.getElementById("div1").style.display = "";
40 }
41 else {
42 alert("您的IP地址拒绝访问网站!");
43 window.location.href = "http://www.baidu.com";
44 }
45 });
46 });
47 </script>
备注:html文件页面div1嵌套

 

转载于:https://www.cnblogs.com/wdd812674802/p/9485470.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值