Several codes for preventing hotlinking in PHP_PHP tutorial

WBOY
Release: 2016-07-13 16:56:19
Original
895 people have browsed it

The anti-hotlink code we describe here only focuses on PHP solutions. Of course, if you have server management rights or htaccess file operations, I recommend not to use PHP anti-hotlink code.

Let’s look at the simplest one first
The following is the code implemented in php. xxx.mp3 is the actual address of your music file. When distributing it to the outside world, only the php address is disseminated, and the mp3 address is not disclosed to the outside world

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

if(strpos($_SERVER['HTTP_REFERER'], 'qq.com') !== FALSE) {

header('HTTP/1.1 404 Not Found');
代码如下 复制代码
/**
* @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']);
?>
exit;

}

readfile('xxx.mp3');
 代码如下 复制代码

$ADMIN[defaulturl] = "http://www.bKjia.c0m/404.htm";//盗链返回的地址
$okaysites = array("http://www.bKjia.c0m/","http://bKjia.c0m"); //白名单
$ADMIN[url_1] = "http://www.bKjia.c0m/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]");
}

?> 

?>

How to add to the whitelist

The code is as follows
Copy code
/** * @author seraphim * @copyright 2012 */ $ADMIN = array( 'defaulturl'=> 'http://www.xx.com/images/banner-header.gif', //The address returned by the hotlink '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', //Whitelist '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']); ?> Instance 3 [Support whitelist 2]
The code is as follows Copy code
<🎜>$ADMIN[defaulturl] = "http://www.bKjia.c0m/404.htm";//The address returned by the hotlink<🎜> $okaysites = array("http://www.bKjia.c0m/","http://bKjia.c0m"); //Whitelist <🎜> $ADMIN[url_1] = "http://www.bKjia.c0m/download/";//Download location 1<🎜> $ADMIN[url_2] = "";//Download location 2, and so on<🎜> <🎜>$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]");<🎜> }<🎜> <🎜>?> There are many ways to implement anti-hotlinking. This article only talks about using PHP to implement it. For example, some sites generate HTML, which is not very convenient. We can operate it in iis, apache, and htaccess. http://www.bkjia.com/PHPjc/631618.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631618.htmlTechArticleThe anti-hotlink code we describe here only focuses on PHP solutions. Of course, if you have server management rights or htaccess I recommend not using PHP hotlink protection for file operations. Let’s take a look at the most...
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template