Summary of common methods for PHP to prevent hotlinking

  • 2020-03-31 20:50:41
  • OfStack

1. Simple anti-hotlinking
 
$ADMIN[defaulturl] = "http://jb51.net/404.htm";//The address returned by hotlinking
$okaysites = array("http://jb51.net/","//www.jb51.net"); // White list  
$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]"); 
} 

?> 

Use method: save the above code as dao4.php,
For instance I tested validatecode. Rar in my site, http://jb51.net/temp/download,
The download connection is represented by the following code.


CODE: [Copy to clipboard]
The file name? Site = 1 & file = file

2. Server against hotlinking
Use iis anti - hotlinking software, you can search, there are a lot of online. There are some in s.jb51.net

3. Anti-hotlinking method of software download

 
//The relative directory of the root directory where the downloaded software is placed relative to the current script directory
$fileRelPath = "../../software"; 
//The exception allows links to the url, note: the domain name itself does not need to fill in, set as sure can download,
//An empty string ("") indicates that the url is entered directly for 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)){ 
?> 

Related articles: