您的位置:首页 > 理论基础 > 计算机网络

HTTP协议分析系列(七)------http协议之refer防盗链

2017-01-22 15:54 176 查看
版权声明:本文为博主原创文章,未经博主允许不得转载。

为了减少流量,引用别的网站的图片,但是有的引进之后不能访问,报404错误。

直接访问:拿qq空间为例



在localhost上引用该图片



像上图中这种效果,当我们在网页引用站外的图片时,常出现这种情况。

服务器是怎样知道这个图片是在站外被引用的呢?

在网站的统计结果里面,统计访问从何而来?统计时候,是如何得知用户从哪来到的本网站呢?



在浏览器里面直接访问www.baidu.com

分析请求头


在本地加入链接

[html]
view plain
copy

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  
</head>  
<body>  
    <img src="http://a2.qpic.cn/psb?/V14OpapO2urokM/8xXI9QF1uxP*p*MGBCRFG2JCDNdsWG5qPkuimqr7K1A!/b/dK7R1VjlFAAA&ek=1&kp=1&pt=0&bo=wgGlAgAAAAABAEM!&sce=0-12-12&rf=viewer_311">  
    <a href='http://www.baidu.com'>访问百度</a>  
</body>  
</html>  


小伙伴们发现了什么?在这里多了一行?啥意思呢,别急,娓娓道来

在HTTP协议中有一个重要的选项:Refer

Refer:代表网页的来源,及上一页的地址

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

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

 

具体如何封杀的站外链接

问题:如何配置Apache服务器用来图片防盗链?

原理:可以再HTTP协议层面,在web服务器层面,根据http协议的referer头信息来判断,如果来自站外,则统一重写到一个很小的防盗链提醒图片上去。

 

具体步骤:

1.打开Apache重写模块mod_rewrite



前面的“#”去掉,并重启Apache



1.在需要防盗链的网站目录下写.htaccess文件,

并指定防盗链规则?

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

 

重写规则:

那种情况重写:

是jpg/jpeg/gif/png图片时

是referer不是与localhost不匹配时

重写

 

怎么重写?

统一rewrite到某个防盗链图片

新建.htaccess文件,写入以下内容

网页文件

[html]
view plain
copy

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  
<html xmlns="http://www.w3.org/1999/xhtml">  
  
<head>  
  
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  
   
  
</head>  
  
<body>  
  
<img src="http://a2.qpic.cn/psb?/V14OpapO2urokM/8xXI9QF1uxP*p*MGBCRFG2JCDNdsWG5qPkuimqr7K1A!/b/dK7R1VjlFAAA&ek=1&kp=1&pt=0&bo=wgGlAgAAAAABAEM!&sce=0-12-12&rf=viewer_311">  
  
<img src='./apple.jpg'>  
  
<a href='http://www.baidu.com'>访问百度</a>  
  
</body>  
  
</html>  


不是本站的用户表示重写

[html]
view plain
copy

RewriteEngine On  
  
RewriteCond %{REQUEST_FILENAME} .*\.(jpg|jpeg|gif|png) [NC]  
  
RewriteCond %{HTTP_REFERER} !localhost [NC]  
  
RewriteRule .* http://www.zixue.it/static/image/common/zixuelogo.png  



[html]
view plain
copy

RewriteEngine On  
RewriteCond %{REQUEST_FILENAME} .*\.(jpg|jpeg|gif|png) [NC]  
RewriteCond %{HTTP_REFERER} !localhost [NC]  
RewriteRule .* no.png  



在那个路径下面

[html]
view plain
copy

RewriteEngine On  
  
Rewrite Base /refer  
  
RewriteCond %{REQUEST_FILENAME} .*\.(jpg|jpeg|gif|png) [NC]  
  
RewriteCond %{HTTP_REFERER} !localhost [NC]  
  
RewriteRule .* no.png  

[php]
view plain
copy

采集别人的图片  
<?php   
require('./http.class.php');  
$http=new Http('http://localhost/refer/apple.jpg');  
$http->setHeader('Referer:http://localhost');  
$res=$http->get();  
file_put_contents('./aa.jpg',substr(strstr($res,"\r\n\r\n"),4));  
//此程序有不完善的地方--应该判断路径或response的mime的头信息,确定图片类型  
echo 'ok';  
?> 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: