PHP实现网页防盗链基础效果

<?php

//防盗链域名,多个用|隔开,如:123.com|abc.com 关闭请留空
define('FDL_URL','www.baidu.com|baidu.com');
$ts="程序开启防盗链";

//判断域名防盗链
if(!is_referer(FDL_URL)){
    header('HTTP/1.1 403 Forbidden');
exit('<h2><font color="black"><center>'.$ts.'</center></font></h2>'); //未授权域名提示
}



//判断防盗链域名
function is_referer($domain){
    //没有设置防盗链
    if($domain=='') return true;
    $referer = strtolower($_SERVER['HTTP_REFERER']);
    $agent = strtoupper($_SERVER['HTTP_USER_AGENT']);
    //部分手机浏览器没有来路
    if(empty($referer)){
        if(preg_match("/(iPhone|iPad|iPod|Android|Linux)/i", $agent)){
            return true;
        }
    }else{
        //开始验证
        $ext = explode("|",$domain);
        for($i=0;$i<count($ext);$i++){
            if(strpos($referer,strtolower($ext[$i])) !== FALSE ){
                return true;
            }
        }
    }
    return false;
}

?>

放入php程序头部可实现基础防盗效果

posted @ 2022-03-23 22:59  富瑞阿得  阅读(105)  评论(0编辑  收藏  举报