search
Homephp教程php手册PHP图片防盗链工具:Hotlink Protection

Hotlink Protection的原理是利用Apache的RewriteCond功能,在得到文件请求时检测请求REFERER,只有是本网站的URL才放行。这个原理很适合自制头像的保护,因为头像只应该在本网站内部使用。

下面以discuz为例,说一下使用方法:烈……火……网……防……盗……链

Discuz的内置头像保存在/forum/images/avatars/目录下,我只想保护这些内置头像,用户上传的头像和其他图片不去限制别人引用,因此RewriteCond代码放置在此目录即可:

Copy to Clipboard引用的内容:[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]

四个RewriteCond排除四种访问自己网站的方法,分别是不带www访问网站下某页面、不带www访问网站首页、带www访问网站下某页面和带www访问网站首页。符合这四种情况外的HTTP_REFERER,并且文件以jpg或jpeg、gif、png结尾则转到http://www.bkjia.com/img/hp.gif文件处。

把这个文件保存为.htaccess,上传到/forum/images/avatars/目录下即可。如想保护整个网站的图片则上传到根目录;如想保护其他格式的文件则直接在RewriteRule中添加扩展名。

顺便说一句:这种方法有一定的局限性,比如某些下载工具可以发送自定的“引用”值;此外有些用户安装的防火墙、杀毒软件会阻止浏览器发送HTTP_REFERER值给服务器,因此他们正常浏览会受影响。真不知道这类防火墙软件厂商是怎么想的,因为这最多只涉及隐私(而且主要是HTTP_REFERER网站的隐私,跟访问者没什么关系),而网络安全无关。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function