Home > php教程 > php手册 > body text

禁止图片盗链

WBOY
Release: 2016-06-21 09:09:54
Original
830 people have browsed it

1、假设充许连结图片的主机域名为:www.test.com

2、修改httpd.conf

 SetEnvIfNoCase Referer "^http://www.test.com/" local_ref=1

Order Allow,Deny
Allow from env=local_ref
 
 


这个简单的应用不光可以解决图片盗链的问题,稍加修改还可以防止任意文件盗链下载的问题。

使用以上的方法当从非指定的主机连结图片时,图片将无法显示,如果希望显示一张“禁止盗链”的图片,我们可以用mod_rewrite 来实现。

首先在安装 apache 时要加上 --enable-rewrite 参数加载 mod_rewrite 模组。

假设“禁止盗链”的图片为abc.gif,我们在 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] 
 


当主机的图片被盗链时,只会看到 abc.gif 这张“禁止盗链”的图片!



Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!