Home  >  Article  >  Backend Development  >  How to prohibit theft links of image files in PHP_PHP Tutorial

How to prohibit theft links of image files in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 17:36:39862browse

1. Assume that the host domain name allowed to link images is: www.test.com

2. Modify httpd.conf

SetEnvIfNoCase Referer "^http://www.test.com/" local_ref=1

Order Allow,Deny
Allow from env =local_ref


This simple application can not only solve the problem of hotlinking of pictures, but with slight modifications can also prevent the problem of hotlinking of arbitrary files.

When using the above method to link images from a non-specified host, the image will not be displayed. If you want to display a "hot link prohibited" image, we can use mod_rewrite to achieve this.

First, when installing apache, add the --enable-rewrite parameter to load the mod_rewrite module.

Assuming that the "Prohibited Hotlinking" image is abc.gif, we can configure it in httpd.conf like this:

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www.)?test.com /.*$ [NC]
RewriteRule . (gif|jpg)$ http://www.test.com/abc.gif [R,L]

When the host's picture is stolen, you will only see abc.gif this "forbidden" Hotlink" picture!

An anti-theft PHP code

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/486630.htmlTechArticle1. Assume that the host domain name allowed to link images is: www.test.com 2. Modify httpd.conf SetEnvIfNoCase Referer "^http://www.test.com/" local_ref=1 FilesMatch ".(gif|jpg)" Order Allow,De...
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