PHP How to disable the hotlinking _php tutorial for picture files

Source: Internet
Author: User

1. Assume that the host domain name of the linked image is: www.test.com

2, modify the 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 picture hotlinking problem, a little modification can also prevent any file hotlinking download problem.

Using the above method when the image from a non-specified host link, the picture will not be displayed, if you want to display a "no hotlinking" picture, we can use mod_rewrite to achieve.

First, when installing Apache, add the--enable-rewrite parameter to load the Mod_rewrite module.

Assuming that the "Prohibit hotlinking" picture is abc.gif, we can configure this in httpd.conf:

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 hotlinking, only then will see abc.gif this "prohibits hotlinking" the picture!

A block of anti-theft PHP code

$ADMIN [Defaulturl] = "http://www.163.com/404.htm";//Address returned by hotlinking
$okaysites = Array ("http://www.163.com/", "http://163.com"); White List
$ADMIN [Url_1] = "http://www.163.com/download/";//Download Location 1
$ADMIN [url_2] = "";//download location 2, etc.
$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]");
}
?>

How to use: Save the above code as dao4.php

http://www.bkjia.com/PHPjc/486630.html www.bkjia.com true http://www.bkjia.com/PHPjc/486630.html techarticle 1, assume that the host domain name of the connection picture is: www.test.com 2, modify httpd.conf setenvifnocase Referer "^http://www.test.com/" local_ref=1 FilesMatch ". (gif|jpg) "Order allow,de ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.