台州温岭做网站请找我们 (专业建站 | 诚信服务) 13958630945 在线客服 联系技术

佳源设计|台州网站建设|佳源网页设计

网站当前位置: 首页>>做网站步骤>>防盗链与反防盗链
建站流程
 
建站知识
 
网站建站疑难
 
网站设计_图象处理
 
网站设计_Css样式
 
网站建设_Flash疑难
 
手机网站建设
 
 建站知识搜索
 

台州网站建设
 
网站优化
 
提交疑问
 做网站步骤 > 防盗链与反防盗链

防盗链与反防盗链

117次浏览
2021-10-19
防盗链规则

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} .*\.(jpg|jpeg|png|gif) [NC]
RewriteCond %{HTTP_REFERER} !xxx.com [NC]
RewriteRule .* error.png
封装的Http请求类
<?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);
  }
}
//测试get
// $http = new Http("http://news.163.com/16/0915/10/C10ES2HA00014PRF.html");
// $result = $http->get();
// echo $result;
//测试post
/*set_time_limit(0);
$str = 'abcdefghijklmnopqrstuvwxyz0123456789';
while(true){
  $http = new Http("http://211.70.176.138/yjhx/message.php");
  $str = str_shuffle($str);
  $username = substr($str,0,5);
  $email = substr($str,5,10).'@qq.com';
  $content = substr($str,10);
  $message = "发表";
  $http->post(array('username'=>$username,'email'=>$email,'content'=>$content,'message'=>$message));
  //sleep(0.1);
}*/
?>

调用代码

<?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";
?>

转载自:https://shimo.im/docs/KcrgrdtVcJWVGG6D/read
页面所在本站地址: http://www.52-life.net/N_no_referer.htm
上一篇 >>php 显示字符串中的隐藏字符
下一篇 >>iframe内嵌视频无法全屏