Two PHP anti-hotlinking methods

小云云
Release: 2023-03-17 08:34:01
Original
1606 people have browsed it

1. Simple hotlink prevention

The code is as follows:

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


How to use: Save the above code as dao4.php,
For example, I used it for testing validatecode.rar is in my site http://jb51.net/temp/download,
uses the following code to represent the download connection.
CODE: [Copy to clipboard]
File name?site=1&file =File

2. Anti-hotlinking method for software downloads

The code is as follows:

//放置下载软件的根目录相对于当前脚本目录的相对目录 $fileRelPath = "../../software"; //例外允许连接的网址,注意:自身域名不需要填入,设定为肯定可以下载, // 空字符串("")表示直接输入网址下载的情况 $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)) ?>
Copy after login


The above is the detailed content of Two PHP anti-hotlinking methods. 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
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!