Home> php教程> php手册> body text

php防盗链几种代码

WBOY
Release: 2016-06-02 09:15:00
Original
1081 people have browsed it
我们这里讲述的防盗链代码只专注于php的解决方案,当然如果你有服务器管理权限或htaccess文件操作我建义不要用php防盗链哦。

先来看个最简单的
下面是php实现的代码, xxx.mp3就是你的音乐文件的实际地址, 对外传播的时候只传播php地址,mp3地址对外不公开

代码如下 复制代码
if(strpos($_SERVER['HTTP_REFERER'], 'qq.com') !== FALSE) {
header('HTTP/1.1 404 Not Found');
exit;
}
readfile('xxx.mp3');
?>

可以加入白名单的做法

代码如下 复制代码
/**
* @author seraphim
* @copyright 2012
*/
$ADMIN = array(
'defaulturl'=> 'http://www.xx.com/images/banner-header.gif', //盗链返回的地址
'url_1' => 'http://www.xx.net/file',
'url_2' => 'http://www.xx.net/file1',
);
$okaysites = array(
'http://box.baidu.com',
'http://tieba.baidu.com/p/1493336008', //白名单
'http://www.xx.com/1.html',
);
$reffer = $_SERVER['HTTP_REFERER'];
if ($reffer) {
$yes = 0;
while (list($domain, $subarray) = each($okaysites)) {
if (ereg($subarray, "$reffer")) {
$yes = 1;
}
}
$theu = 'url_' . $_GET['site'];
$file = $_GET['file'];
if ($ADMIN[$theu] and $yes == 1) {
header("Location: $ADMIN[$theu]/$file");
} else {
header("Location: $ADMIN[defaulturl]");
}
} else {
header("Location: $ADMIN[defaulturl]");
}
print_r($_SERVER['HTTP_REFERER']);
?>

实例3 [支持白名单二]

代码如下 复制代码

$ADMIN[defaulturl] = "http://www.phprm.com/404.htm";//盗链返回的地址
$okaysites = array("http://www.phprm.com/","http://phprm.com"); //白名单
$ADMIN[url_1] = "http://www.phprm.com/download/";//下载地点1
$ADMIN[url_2] = "";//下载地点2,以此类推

$reffer = $HTTP_REFERER;
if($reffer) {
$yes = 0;
while(list($domain, $subarray) = each($okaysites)) {
if (ereg($subarray,"$reffer")) {
$yes = 1;
}
}
$theu = "url"."_"."$site";
if ($ADMIN[$theu] AND $yes == 1) {
header("Location: $ADMIN[$theu]/$file");
} else {
header("Location: $ADMIN[defaulturl]");
}
} else {
header("Location: $ADMIN[defaulturl]");
}

?>

还有很多的方法来实现防盗链本文章只讲到了利用php实现,像有些站生成了html这样做起来就不怎么方便了,我们可以在iis,apache,htaccess来操作。



本文地址:

转载随意,但请附上文章地址:-)

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!