Home  >  Article  >  Backend Development  >  Summary of common methods to prevent hotlinking in PHP_PHP tutorial

Summary of common methods to prevent hotlinking in PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:36:31873browse

1. Simple hotlink protection

Copy code The code is as follows:

$ADMIN[defaulturl] = "http://jb51. net/404.htm";//The address returned by the hot link
$okaysites = array("http://jb51.net/","http://www.jb51.net"); //Whitelist
$ADMIN[url_1] = "http://jb51.net/temp/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]");
}

? >

How to use: Save the above code as dao4.php,
For example, the validatecode.rar I used for testing is in my site http://jb51.net/temp/download ,
uses the following code to represent the download link.


CODE: [Copy to clipboard]
File name?site=1&file=file

2. Server anti-hotlinking
To use iis anti-hotlinking software, you can search for it. There are many online. There are downloads in s.jb51.net

3. Anti-hotlinking method for software downloads

Copy the code The code is as follows:

//Place the root directory of the downloaded software relative to the current script directory
$fileRelPath = "../../software";
//Exception allowed URL for connection , Note: You do not need to fill in your own domain name, set it to be sure to download,
// Empty string ("") indicates the situation of directly entering the URL to download
$excludeReferArr = array("www.wjb51.net ", "wjb51.net");

chdir($fileRelPath);
$fileRootPath = getcwd() ."/";

$filePath=$HTTP_GET_VARS["file" ];

$url=parse_url($_SERVER["HTTP_REFERER"]);

if($url[host]!=$_SERVER["HTTP_HOST"] && !in_array($referHost , $excludeReferArr)){
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322107.htmlTechArticle1. The simple anti-hotlink copy code is as follows: $ADMIN[defaulturl] = "http://jb51.net /404.htm"; //The address returned by the hotlink $okaysites = array("http://jb51.net/","http://www.jb51.net"); //White...
Statement:
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