Php implements anti-Leech protection for image files and downloaded files

Source: Internet
Author: User
This article describes how to implement anti-Leech protection for image files and downloaded files in php, including common operation methods and server-side configuration methods, which are of great practical value, for more information about how to implement anti-Leech protection for image files and download files in php, see the following example. Share it with you for your reference. The specific analysis is as follows:

The simplest anti-Leech method in php is to use the $ _ SERVER ['http _ referer'] function of php to operate the anti-Leech function. However, this method is not reliable and we still need to use apache in the end, the specific operation method is as follows:

Php anti-Leech:

The code is as follows:

<? Php
Session_start ();
Session_register ('check ');
$ _ SESSION ['check'] = true;
?>


Check the session variable and determine whether to visit the home page. Check whether the source webpage reference (HTTP_REFERER) is from the webpage of the local website.

The method is as follows:

The code is as follows:

<? Php
Session_start ();
$ Refs = parse_url ($ _ SERVER ['http _ referer']); // refer to webpage information for decomposition.
// Check whether the home page session and source host are the same
If (! ($ _ SESSION ['check']) | $ refs ['host']! = $ _ SERVER ['http _ host'])
Exit;
?>

Note: this can only be a simple anti-Leech protection. if you understand it a little bit, you can crack it,

Use the server to set php anti-theft connection

Apache anti-Leech:

Modify httpd. conf:

The code is as follows:

SetEnvIfNoCase Referer "^" local_ref = 1

Order Allow, Deny
Allow from env = local_ref

If you want to display an image "anti-leeching", you can use mod_rewrite.
Add the -- enable-rewrite parameter to load the mod_rewrite module when installing apache.
Assume that the image of the anti-Leech feature is abc.gif. we can configure it in httpd. conf as follows:

The code is as follows:

RewriteEngine on
RewriteCond % {HTTP_REFERER }! ^ $
RewriteCond % {HTTP_REFERER }! ^ Http: // (www .)? Www.jb51.net/. * $ [NC]
RewriteRule. (gif | jpg) $ abc.gif [R, L]
DocumentRoot "/usr/local/apache/htdocs"
# Set the directory for storing html files of the website.

Options FollowSymLinks
AllowOverride None

Iis anti-Leech:

Select c: RewriteRewrite. dll as the execution file.
Httpd. ini is the configuration file
Image anti-Leech code


The code is as follows:

[ISAPI_Rewrite]
#3600 = 1 hour
CacheClockRate 3600
RepeatLimit 32
# Protect httpd. ini and httpd. parse. errors files
# From accessing through HTTP
RewriteCond Host: (. +)
RewriteCond Referer :(?! Http: // 1 .*).*
RewriteRule .*.(? : Gif | jpg | png | bmp)/force.gif [I, O]

I hope this article will help you with PHP programming.

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.