Referer与防盗链

来源:互联网 发布:虚拟机网络不可用 编辑:程序博客网 时间:2024/05/20 06:26

Referer:HTTP请求头信息中的Referer可以判断访问的本站资源是来自哪里的。因此,我们可以利用Referer来避免别人盗取自己本站的资源,如图片。

如Apache可以编写.htaccess文件来重写访问的资源连接

RewriteEngine On#Rewrite Base /dir #只在dir目录下生效RewriteCond %{REQUEST_FILENAME} .*\.(jpg|jpeg|gif|png) [NC]#重写请求图片文件RewriteCond %{HTTP_REFERER} !localhost [NC] #对HTTP_REFERER不是来自localhost的图片文件则进行重写RewriteRule .*  no.png #重写为no.png图片

既然是通过referer来判断,那么我们可以进行反防盗链措施。
在HTTP请求时伪造Referer信息

<?php  require('./http.class.php');$http = new Http('http://localhost/dog.jpg');//请求该URL的图片资源$http->setHeader('Connection','close');$http->setHeader('Referer','http://localhost');//伪造Referer$res = $http->get();//进行GET请求file_put_contents('./get.png',substr(strstr($res,"\r\n\r\n"),4));//将获取的资源写到get.png文件中?>  

上述require的http.class.php源码:http://blog.csdn.net/whd526/article/details/75070920