AB模板王专注于织梦源码分享、织梦模板、discuz模板、wordpress主题等网站模板,为广大站长提供网站模板以及Discuz插件等素材。
用户名:
密码:
注册

织梦源码,模板王

VIP

织梦搜索功能禁止特定地区用户访问

www.adminbuy.cn / 2018-08-11

由于很多人有一些需求在做国内站的时候,不做国内某个地区的业务的时候,他想屏蔽用户访问网站,我这边以dedecms搜索功能为例子,因为是以前开发的,时间比较有限现在,不重新调整做整站的功能,如果有需要,可以将以下相关代码放入一个php文件,然后在data/common.ini.php里面包含就可以了。

 

目前网络上有很多资源,例如著名的珊瑚虫ip数据库,但是其文件在本地,得定时更新。我们现在使用淘宝ip数据库,通过json返回的数据来解析ip是否某地区,下面以"上海市"为例:

首页找到dedecms底下plus下的search.php文件,查找以下代码

1 if($cfg_notallowstr !='' && preg_match("#".$cfg_notallowstr."#i", $keyword))
2 {
3     ShowMsg("你的搜索关键字中存在非法内容,被系统禁止!","-1");
4     exit();
5 }

在上面加入

 

01 function get_ip_data(){  
02 $ip=file_get_contents("http://ip.taobao.com/service/getIpInfo.php?ip=".get_client_ip());
03     $ip = json_decode($ip);
04     if($ip->code){
05         return false;
06     }
07     $data = (array) $ip->data;
08     if($data['city']=='上海市' && !isCrawler()){
09        // exit('http://www.adminbuy.cn');
10   return true;
11          
12 /*     header("location:".getenv("HTTP_REFERER")); // 返回其调用页面 */
13   
14     }
15 }
16   
17 function isCrawler() {
18         $spiderSite= array(
19                         "TencentTraveler",
20                         "Baiduspider+",
21                         "BaiduGame",
22                         "Googlebot",
23                         "msnbot",
24                         "Sosospider+",
25                         "Sogou web spider",
26                         "ia_archiver",
27                         "Yahoo! Slurp",
28                         "YoudaoBot",
29                         "Yahoo Slurp",
30                         "MSNBot",
31                         "Java (Often spam bot)",
32                         "BaiDuSpider",
33                         "Voila",
34                         "Yandex bot",
35                         "BSpider",
36                         "twiceler",
37                         "Sogou Spider",
38                         "Speedy Spider",
39                         "Google AdSense",
40                         "Heritrix",
41                         "Python-urllib",
42                         "Alexa (IA Archiver)",
43                         "Ask",
44                         "Exabot",
45                         "Custo",
46                         "OutfoxBot/YodaoBot",
47                         "yacy",
48                         "SurveyBot",
49                         "legs",
50                         "lwp-trivial",
51                         "Nutch",
52                         "StackRambler",
53                         "The web archive (IA Archiver)",
54                         "Perl tool",
55                         "MJ12bot",
56                         "Netcraft",
57                         "MSIECrawler",
58                         "WGet tools",
59                         "larbin",
60                         "Fish search",
61                 );
62         if(in_array(strtolower($_SERVER['HTTP_USER_AGENT']),$spiderSite)){
63             return true;
64         }else{
65             return false;
66         }
67 }
68   
69 //获取客户端ip
70 function get_client_ip()
71 {
72     if (isset($_SERVER)){
73             if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])){
74                 $realip = $_SERVER["HTTP_X_FORWARDED_FOR"];
75             else if (isset($_SERVER["HTTP_CLIENT_IP"])) {
76                 $realip = $_SERVER["HTTP_CLIENT_IP"];
77             else {
78                 $realip = $_SERVER["REMOTE_ADDR"];
79             }
80     else {
81             if (getenv("HTTP_X_FORWARDED_FOR")){
82                 $realip = getenv("HTTP_X_FORWARDED_FOR");
83             else if (getenv("HTTP_CLIENT_IP")) {
84                 $realip = getenv("HTTP_CLIENT_IP");
85             else {
86                 $realip = getenv("REMOTE_ADDR");
87             }
88         }
89     return $realip;
90 }
91 if(get_ip_data())
92 {
93     ShowMsg("你的IP被禁止!","-1");
94     exit();
95 }

 

保存文件后,你让上海市的朋友访问,系统就会提示"你的IP被禁止!"的提示 

本文由AB模版王整理发布,转载请说明出处:http://www.adminbuy.cn
收缩