Php implements anti-leech protection for image files and downloaded files,
This example describes how php implements anti-leech protection for image files and downloaded files. 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:
Copy codeThe 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:
Copy codeThe 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:
Copy codeThe Code is as follows: SetEnvIfNoCase Referer "^" local_ref = 1
<FilesMatch ". (gif | jpg)">
Order Allow, Deny
Allow from env = local_ref
</FilesMatch>
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:
Copy codeThe 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.
<Directory/>
Options FollowSymLinks
AllowOverride None
</Directory>
Iis anti-leech:
Select c: RewriteRewrite. dll as the execution file.
Httpd. ini is the configuration file
Image anti-leech code
Copy codeThe 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.