可以将代码include在网站的首页文件中,目前用的IP库是太平洋的之前网上都在用的淘宝的IP库已失效。最终可实现备案管理局所在市IP访问网站时为空白页但通过BAIDU搜索进来的或BAIDUSPIDER仍然可用。
<?php
header("Content-type: text/html; charset=utf-8");
$verification = 'XXX市';
$ip = $_SERVER['REMOTE_ADDR'];
$antecedents = $_SERVER['HTTP_REFERER'];//访客来路地址
$result = file_get_contents("http://whois.pconline.com.cn/ipJson.jsp?ip=".$ip."&json=true");
$result = iconv("gb2312", "utf-8//IGNORE",$result);//太平返回乱码BY JI
$address = json_decode($result,true);
//判断访客是否属于XX,是否来自百度,是否来自谷歌
if($address['city'] == $verification && strpos($antecedents, 'baidu') === false && strpos($antecedents, 'google') === false){
die();//
}
2024更新,使用太平洋IP地址被坑了,可能因为用的人太多了服务经常不可用,导致管局审核那天打开了网站备案被退回,改用了其他IP地址库,因为返回格式不太一样,后面代码也有修改,记录下,代码保存在了主题根目录下的beian.php中,下次直接用吧!
<?php
// 备案用
header("Content-type: text/html; charset=utf-8");
$verification = '石家庄';
$ip = $_SERVER['REMOTE_ADDR'];
$antecedents = $_SERVER['HTTP_REFERER'];//访客来路地址
$result = file_get_contents("https://webapi-pc.meitu.com/common/ip_location?ip=" . $ip);
$address = json_decode($result,true);
//获取城市信息
//$city = $address['data'][key($address['data'])]['city'];
//$city简写版
$city = reset($address['data'])['city'];
//判断访客是否属于XX,是否来自百度,是否来自谷歌
if($city == $verification && strpos($antecedents, 'baidu') === false && strpos($antecedents, 'google') === false){
die();
}
20250428更新,美图的IP库也挂了更换IP库并重写逻辑,之前的判断是否来自百度或GOOGLE搜索的用户逻辑其实没有用,访客虽然能访问搜索进来的页面但一旦进入下一个页面还是会被屏蔽因为这时REFERER发生了变化。所以删除该逻辑,并加上对各大蜘蛛的放行:
<?php
// 备案用
header("Content-type: text/html; charset=utf-8");
$verification = 'CITY'; // 注意,这里改成了英文
$ip = $_SERVER['REMOTE_ADDR'];
// 新API请求
$result = @file_get_contents("ip地址库自己找吧不在这里分享给大家了经常挂用的人多了也挂:)" . $ip);
// 解析JSON
$address = json_decode($result, true);
// 获取城市信息
$city = isset($address['city']) ? $address['city'] : '';
// 获取用户的 User-Agent
$user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
// 判断是否为常见搜索引擎的爬虫
$is_search_engine_bot = (
strpos($user_agent, 'Baiduspider') !== false ||
strpos($user_agent, 'Googlebot') !== false ||
strpos($user_agent, 'Bingbot') !== false ||
strpos($user_agent, 'Sogou') !== false ||
strpos($user_agent, '360Spider') !== false ||
strpos($user_agent, 'Bytespider') !== false
);
// 判断访客是否属于CITY,且不是爬虫
if ($city === $verification && !$is_search_engine_bot) {
die();
}
?>
20250429最终优化方案更新,每次都要查询IP并验证导致网站速度很慢,所以新增COOKIE在客户端保存已验证过的非某地的用户的信息,有效期一天,期间都无需重复验证,另外直接放行蜘蛛,in the future can also add wp_is_mobile() to let go the mobile users.The bureaus pefer pc so no need to block users who visit the page via mobile! 最终版:
<?php
// 备案用
header("Content-type: text/html; charset=utf-8");
$verification = 'Shijiazhuang';
$ip = $_SERVER['REMOTE_ADDR'];
// 获取 User-Agent
$user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
// 判断是否为爬虫
$is_search_engine_bot = (
strpos($user_agent, 'Baiduspider') !== false ||
strpos($user_agent, 'Googlebot') !== false ||
strpos($user_agent, 'Bingbot') !== false ||
strpos($user_agent, 'Sogou') !== false ||
strpos($user_agent, '360Spider') !== false ||
strpos($user_agent, 'Bytespider') !== false
);
// 判断是否为移动端(直接使用 wp_is_mobile,无需检查函数是否存在)
$is_mobile = wp_is_mobile();
// 如果已经验证过、是爬虫、或者是移动端,就直接跳过
if (
(isset($_COOKIE['ip_checked']) && $_COOKIE['ip_checked'] === '1') ||
$is_search_engine_bot ||
$is_mobile
) {
return;
}
// 发送IP验证请求
$result = @file_get_contents("https://api.ip.sb/geoip/" . $ip);
$address = json_decode($result, true);
$city = isset($address['city']) ? $address['city'] : '';
// 如果来自特定城市则阻止访问
if ($city === $verification) {
die();
}
// 如果没有DIE()证明用户非来自SHIJIAZHAUNG,则脚本继续运行设置 cookie,有效期设置为1天
setcookie('ip_checked', '1', time() + 86400, "/", "", true, true); // secure + httponly
?>
参考:https://github.com/ihmily/ip-info-api?tab=readme-ov-file#address-1.2
本文介绍了一种基于PHP的IP访问限制方案,通过对访问者IP进行地理位置判断,并结合来路地址判断,实现特定地区直接显示空白页面,同时允许搜索引擎爬虫正常访问。

被折叠的 条评论
为什么被折叠?



