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.youkuaiyun.com/whd526/article/details/75070920