HTTP协议学习笔记之(五)referer头与防盗链
有这样一种情况:就是有时候我们会在自己的网站引用站外的图片,但去引用不了,那么问题来了,服务器是怎么知道图片是在站外被引用的呢?
还有在网站的统计结果,统计用户从何而来,比如外部链接
在Http协议中,头信息里有一个重要的选项:Referer,它代表网页的来源即上一页的地址。如果是直接在浏览器上输入地址,进入网站,则没有Referer,这也是为什么服务器知道我们的图片是从哪引用的
那么,如何配置apache服务器,用于图片防盗链?
原理:在Web服务器层面,根据Http协议的referer头信息来判断,如果来自站外,则统一重写到一个很小的防盗链提醒图片上去。
具体步骤:
1、打开apache重写模块,mod_rewrite
2、在需要防盗链的网站或目录,写.htaccess文件,并指定防盗链规则
如何指定?
分析referer信息,如果不是来自本站,则重写
重写规则:
那种情况重写:
图片是jpeg/jpg/gif/png图片时
referer头与网站域名不匹配时
怎么重写:
统一rewrite到某个防盗链图片
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} .*\.(jpg|jpeg|gif|png) [NC]
RewriteCond %{HTTP_REFERER} !localhost [NC]
RewriteRule .* http://a3.qpic.cn/psb?/V106k4AG0SBkbJ/UHPZuXzLHdsn1XOApfpGVqYb.0baVUcNxgXW90GO0rQ!/m/dFYBAAAAAAAA&ek=1&kp=1&pt=0&bo=AAXUAwAAAAAAAPQ!&tm=1487926800&sce=60-3-3&rf=0-0
防盗链的目的:减轻服务器的负担
<?php
/**
PHP+scoket编程 发送HTTP请求
功能:模拟下载,注册,登录,批量发帖
**/
//http请求类接口
interface Prote{
//连接url
function conn($url);
//发送get查询
function get();
//发送post查询
function post();
//关闭连接
function close();
}
class Http implements Prote{
const CRLF = "\r\n"; //声明一个换行常量
protected $errno = -1;
protected $errstr = '';
protected $response = '';
protected $fh = null;
protected $url = array(); //存放url信息
protected $line = array(); //放置请求行
protected $header = array(); //放置头信息
protected $body = array(); //放置主体信息
public function __construct($url){
$this->conn($url);
$this->setHeader('Host:' . $this->url['host']);
}
/**
* [setLine 此方法负责写请求行]
* @param [str] $method [description]
*/
protected function setLine($method){
//$this->line[0] = $method . ' ' .$this->url['path'] . '?' . $this->url['query'] . ' ' . 'HTTP/1.1';
$this->line[0] = $method . ' ' .$this->url['path'] . ' ' . 'HTTP/1.1';
}
/**
* [setHeader 此方法负责写头信息]
* @param [type] $headerline [头信息]
*/
public function setHeader($headerline){
$this->header[] = $headerline;
}
//此方法负责写主体信息
protected function setBody($body){
$this->body[] = http_build_query($body);
}
/**
* [conn 连接url]
* @param [type] $url [请求的地址]
* @return [type] [description]
*/
public function conn($url){
$this->url = parse_url($url); //分析$url
// 判断端口
if(!isset($this->url['port'])){
$this->url['port'] = 80;
}
if(!isset($this->url['query'])){
$this->url['query'] = '';
}
$this->fh = fsockopen($this->url['host'],$this->url['port'],$this->errno,$this->errstr,3);
}
/**
* [get 构造get请求的数据]
* @return [type] [description]
*/
public function get(){
$this->setLine('GET');
$this->request();
return $this->response;
}
/**
* [post 构造post查询的数据]
* @return [type] [description]
*/
public function post($body = array()){
$this->setLine('POST');
// 设置content-type
$this->setHeader('Content-type:application/x-www-form-urlencoded');
//构造主体信息 ,比GET不一样的地方
$this->setBody($body);
//计算content-length
$this->setHeader('Content-length: ' . strlen($this->body[0]));
$this->request();
return $this->response;
}
/**
* [request 真正请求]
* @return [type] [description]
*/
public function request(){
//把请求行、头信息、实体信息 放在一个数组中,便于拼接
$req = array_merge($this->line,$this->header,array(''),$this->body,array(''));
$req = implode(self::CRLF,$req);
/**
* 请求信息拼接结果
* GET /17/0223/08/CDUQ91V100097U7T.html HTTP/1.1
* Host: tech.163.com
*
*
*/
fwrite($this->fh,$req);
while(!feof($this->fh)){
$this->response .= fread($this->fh,1024);
}
$this->close(); //关闭连接
}
/**
* 换行:
* linux:\r
* mac : \n
* window : \r\n
* http标准协议:\r\n
*/
//关闭连接
public function close(){
fclose($this->fh);
}
}
/**
* 采集百度空间防盗链图片
*/
$url = "http://qlogo2.store.qq.com/qzone/810677141/810677141/100?1376376280";
$http = new Http($url);
$http->setHeader('Referer: http://qlogo2.store.qq.com'); //伪造Referer
$res = $http->get();
echo $res;exit;
file_put_contents('./a.jpeg',substr(strstr($res,'\r\n\r\n'),4));

浙公网安备 33010602011771号