Home > Backend Development > PHP Tutorial > Anti-theft Summary of common methods to prevent hotlinking in PHP

Anti-theft Summary of common methods to prevent hotlinking in PHP

WBOY
Release: 2016-07-29 08:43:10
Original
989 people have browsed it

1. Simple anti-hotlinking

Copy the code The code is as follows:


$ADMIN[defaulturl] = "http://jb51.net/404.htm";//The address returned by the hotlinking
$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]");
}
?>


Usage: 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,
The following code is used 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 for it. There are many online. It is available in s.jb51.net
3. Anti-hotlink method for software download

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 allows the connection URL, note: your own domain name does not need to be filled in, set it to be sure to download,
// Empty string ("") means direct input URL download situation
$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)){
?>

The above has introduced a summary of the common methods of anti-theft PHP anti-hotlinking, including anti-theft content. I hope it will be helpful to friends who are interested in PHP tutorials.

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