文章
位置: 首页 >文章
PHP图片防盗链
- PHP
- 2021-09-27
- 2311
- 0
PHP图片防盗链
1、防盗链
防盗链处理主要是用Referer原理,分析了Referer头信息,根据Referer进行防盗链处理
在一些大型网站中的图片采用了防盗链的规则,比如百度贴吧,规则其实主要是该站点请求时,会先判断请求头中的信息,如果请求头中有Referer信息,然后根据规则来判断Referer头信息是否符合要求,Referer 信息是请求该图片的来源地址。
Apache配置:
apache web服务器中开启mod_rewrite模块
#LoadModule rewrite_module modules/mod_rewrite.so 去掉#
在需要防盗的网站或目录中,写.htaccess文件,并指定防盗链规则
新建一个.htaccess文件,在.htaccess文件中利用正则判断并指定规则:
如果是图片资源且referer头信息是来自于本站,则通过
重写规则
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} .*\.(jpg|jpeg|png|gif) [NC]
RewriteCond %{HTTP_REFERER} !localhost [NC]
RewriteRule .* no.png
NGINX配置:
location /images {
valid_referers none blocked www.baidu.com 192.168.200.222 *.example.com example.* www.example.org ~\.google\.;
if ($invalid_referer){
return 403;
}
root /usr/local/nginx/html;
}
2、反防盗链
做爬虫获取图片的时候,我们需要使用防盗链技术伪造一个Referer头信息
<?php
/**
* 下载图片
* @author webbc
*/
require './Http.class.php';//这个类是我自己封装的一个用于HTTp请求的类
$http = new Http("http://localhost/booledu/http/apple.jpg");
//$http->setHeader('Referer:http://tieba.baidu.com/');//设置referer头
$res = $http->get();
$content = strstr($res,"\r\n\r\n");
file_put_contents('./toutupian.jpg',substr($content,4));
echo "ok";
?>
封装http请求class类型
<?php
/**
* Http请求类
* @author webbc
*/
class Http{
const CRTF = "\r\n";
private $errno = -1;
private $errstr = '';
private $timeout = 5;
private $url = null;//解析后的url数组
private $version = 'HTTP/1.1';//http版本
private $requestLine = array();//请求行信息
private $header = array();//请求头信息
private $body = array();//请求实体信息
private $fh = null;//连接端口后返回的资源
private $response = '';//返回的结果
//构造函数
public function __construct($url){
$this->connect($url);
$this->setHeader('Host:'.$this->url['host']);//设置头信息
}
//通过URL进行连接
public function connect($url){
$this->url = parse_url($url);//解析url
if(!isset($this->url['port'])){
$this->url['port'] = 80;
}
$this->fh = fsockopen($this->url['host'],$this->url['port'],$this->errno,$this->errstr,$this->timeout);
}
//设置请求行信息
public function setRequestLine($method){
$this->requestLine[0] = $method.' '.$this->url['path'].' '.$this->version;
}
//设置请求头信息
public function setHeader($headerLine){
$this->header[] = $headerLine;
}
//设置请求实体信息
public function setBody($body){
$this->body[] = http_build_query($body);
}
//发送get请求
public function get(){
$this->setRequestLine('GET');//设置请求行
$this->request();//发送请求
$this->close();//关闭连接
return $this->response;
}
//发送请求
private function request(){
//拼接请求的全部信息
$reqestArr = array_merge($this->requestLine,$this->header,array(''),$this->body,array(''));
$req = implode(self::CRTF,$reqestArr);
//print_r($req);die;
fwrite($this->fh,$req);//写入信息
//读取
while(!feof($this->fh)){
$this->response .= fread($this->fh,1024);
}
}
//发送post请求
public function post($body = array()){
//设置请求行
$this->setRequestLine("POST");
//设置实体信息
$this->setBody($body);
//设置Content-Type
$this->setHeader('Content-Type:application/x-www-form-urlencoded');
//设置Content-Length
$this->setHeader('Content-Length:'.strlen($this->body[0]));
//请求
$this->request();
$this->close();//关闭连接
return $this->response;
}
//关闭连接
public function close(){
fclose($this->fh);
}
}
转载:欢迎来到本站,转载请注明文章出处https://www.ormcc.com ,欢迎加入技术讨论群599606903
上一篇:搭建本地版ChatGPT
下一篇:输入框交互效果(一)
文章评论
- 0
- 0
- 0
- 分享
- 打赏
ormcc
一个爱捣鼓的程序员
- 160
会员 - 87
今日访问 - 607
文章
IP访问121793次,运行1480天
评论排行
文章归档
- 还没有相关文章