Home >Backend Development >PHP Tutorial >How to prohibit theft links of image files in PHP_PHP Tutorial
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 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 An anti-theft PHP code $ADMIN[defaulturl] = "http://www.163.com/404.htm";//The address returned by the hot link Usage: Save the above code as dao4.php
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.
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!
$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]");
}
?>