1.检测来访源,设置站点白名
<?php
$allowedHosts = ['www.ximi.me', 'ximi.me'];
if (!isset($_SERVER['HTTP_REFERER']) || !in_array(parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST), $allowedHosts)) {
header('HTTP/1.0 403 Forbidden');
exit('来源不合法禁止访问,Access forbidden');
}
?>
2.开启跨域访问验证
<?php
header('Access-Control-Allow-Origin: https://www.ximi.me'); // 仅允许特定域访问
?>
3.检测HTTP 头部 是否非正常访问
<?php
if (!isset($_SERVER['HTTP_ACCEPT']) || $_SERVER['HTTP_ACCEPT'] == '*/*') {
header('HTTP/1.0 403 Forbidden');
exit('检测到非法访问;Access forbidden');
}
?>
测试图片:
案例源码: