PHP anti-Leech tool: HotlinkProtection_PHP tutorial

Source: Internet
Author: User
PHP anti-Leech tool: HotlinkProtection. HotlinkProtection uses Apache's RewriteCond function to detect the request REFERER when a file request is obtained. only the URL of this website is allowed. This principle is suitable for the Hotlink Protection of self-made portraits. The principle is to use Apache's RewriteCond function to detect the request REFERER when obtaining file requests. only the URL of this website is allowed. This principle is suitable for the protection of self-made portraits, because portraits should only be used within the website.

The following uses discuz as an example to describe how to use it: lie ...... Fire ...... Network ...... Defense ...... Stealing ...... Chain

The built-in pictures of Discuz are stored in the/forum/images/avatars/Directory. I just want to protect these built-in pictures. the pictures uploaded by users and other pictures do not restrict others' reference, therefore, the RewriteCond code can be placed in this directory:

Copy to ClipboardReference: [www.bkjia.com] RewriteEngine on
RewriteCond % {HTTP_REFERER }! ^ Http://bkjia.com/.#$ [NC]
RewriteCond % {HTTP_REFERER }! ^ Http://bkjia.com $ [NC]
RewriteCond % {HTTP_REFERER }! ^ Http://www.bkjia.com/.#$ [NC]
RewriteCond % {HTTP_REFERER }! ^ Http://www.bkjia.com $ [NC]
RewriteRule. * \. (jpg | jpeg | gif | png) $ http://www.bkjia.com/img/hp.gif [R, NC]

Four RewriteCond methods exclude four methods to access your website, access a webpage without www, access a webpage without www, access a webpage with www, and access a webpage with www. HTTP_REFERER that meets these four conditions, and the file ends with jpg, jpeg, gif, and png, and is transferred to the http://www.bkjia.com/img/hp.gif file.

Save the file as. htaccess and upload it to the/forum/images/avatars/directory. To protect images of the entire website, upload them to the root directory. to protect files of other formats, add the extension in RewriteRule.

By the way, this method has some limitations. for example, some download tools can send custom "reference" values; in addition, some firewall and anti-virus software installed by users will prevent the browser from sending the HTTP_REFERER value to the server, so their normal browsing will be affected. I really don't know what such firewall software vendors think, because it only involves privacy at most (and mainly the privacy of HTTP_REFERER websites, which has nothing to do with visitors), and network security is irrelevant.

The principle of http://www.bkjia.com/PHPjc/364608.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/364608.htmlTechArticleHotlink Protection is to use the RewriteCond function of Apache to detect the request REFERER when getting file requests. only the URL of this website is allowed. This principle is suitable for self-made portraits...

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.