Home > Backend Development > PHP Tutorial > Common methods to prevent hotlinking in php

Common methods to prevent hotlinking in php

怪我咯
Release: 2023-03-13 19:56:01
Original
1454 people have browsed it

Today’s Internet has a lot of collection websites, and many websites like to hotlink/steal pictures from other people’s websites. This not only infringes on network rights, but also causes the websites to be hotlinked to consume a lot of traffic, giving rise to The server puts a lot of pressure on the server. This article introduces two methods on how to prevent image theft/hotlinking in PHP. Friends in need can refer to it.

What is the use of anti-hotlinking for pictures? It prevents other websites from stealing your pictures and wasting your precious traffic. This article introduces you to three methods in PHP to prevent image theft/hotlinking

1. Simple anti-hotlinking, code is as follows:

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

?>
Copy after login

Usage method: Replace the above code Save it as dao4.php,
For example, the validatecode.rar I used for testing is in my site http://www.php.cn/temp/download,
use the following code to represent the download connection.

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

2. Server anti-hotlinking
IIS anti-hotlinking software is used. You can search it. There are many online.

3. How to prevent hotlinking when downloading software
The code is as follows:

//放置下载软件的根目录相对于当前脚本目录的相对目录 
$fileRelPath = "../../software"; 
//例外允许连接的网址,注意:自身域名不需要填入,设定为肯定可以下载, 
// 空字符串("")表示直接输入网址下载的情况 
$excludeReferArr = array(http:/www.php.cn", "php.cn"); 

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)){ 
?>
Copy after login

The above is the detailed content of Common methods to prevent hotlinking in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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