zoukankan      html  css  js  c++  java
  • php防盗链技术

    Http协议中,头信息里,有一个重要的选项: Referer

    Referer: 代表网页的来源,即上一页的地址

    如果是直接在浏览器上输入地址,回来进来,则没有Referer.

    这也是为什么服务器知道我们的图片是从哪儿引用的,也知道我们的客户从哪个网站链接点击过来的.

    问题: 如何配置apache服务器,用于图片防盗链?

    原理: 在web服务器层面,根据http协议的referer头信息,来判断.

    如果来自站外,则统一重写到一个很小的防盗链提醒图片上去.

    具体步骤: 

    1:打开apache 重写模块 mod_rewrite

    前面的”#”去掉,并重启apache

    2:在需要防盗的网站或目录,写.htaccess文件,

    并指定防盗链规则

    如何指定?

    自然是分析referer信息,如果不是来自本站,是重写

    重写规则:

    哪种情况重写:

    是jpeg/jpg/gif/png图片时

    是referer头与localhost不匹配时

    重写

    怎么重写?

    统一 rewirte到 某个防盗链图片

    如下面的例子:

    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} .*.(jpg|jpeg|gif|png) [NC]

    RewriteCond %{HTTP_REFERER} !localhost [NC]

    RewriteRule .*  no.png

     

    配置文件.htaccess

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} .*.(jpg|jpeg|gif|png) [NC]        //表示什么条件下重写,是图片的条件下重写
    RewriteCond %{HTTP_REFERER} !localhost [NC]      //不是localhost访问时需要重写
    RewriteRule .* no.png       //重定向到图片no.png
    

    img.html

    <!DOCTYPE>
    <html>
    <head>
    	<title>图片防盗链技术</title>
    	<meat http-equive="Content-Type" content="text/html;charest=utf-8"/>
    </head>
    	<img src="apple.jpg"/>
    </html>
    

      

  • 相关阅读:
    python 运行CMD和shell命令
    python 装饰器2 常用装饰器【@property,@x.setter,@x.deleter】
    正则表达式 python re正则模块
    Interesting Finds: 2008.01.06
    Interesting Finds: 2008.01.11
    Interesting Finds: 2007.12.25
    Interesting Finds: 2007.12.26
    Interesting Finds: 2008.01.04
    Interesting Finds: 2008.01.03
    Interesting Finds: 2008.01.09
  • 原文地址:https://www.cnblogs.com/yxhblogs/p/4741208.html
Copyright © 2011-2022 走看看