欧美精品免费观看二区/在线观看av/粉嫩国产精品14xxxxx/亚洲精品视频在线观看免费

基于PHP做個圖片防盜鏈
發布時間:2024-03-07

    1、圖片防盜鏈
    在一些大型網站中,比如百度貼吧,該站點的圖片采用了防盜鏈的規則,以至于使用下面代碼會發生錯誤。

    簡單代碼:

    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <title></title>
      <link rel="stylesheet" href="">
    </head>
    <body>
      <!--引用一張百度貼吧的圖片-->
      <img src="http://imgsrc.baidu.com/forum/pic/item/03a4462309f79052204229be04f3d7ca7acbd5d5.jpg"/>
    </body>
    </html>

    出現的問題:


    出錯的原因

    主要是該站點的圖片采用了防盜鏈的規則,其實這個規則也比較簡單, 和大家一說就知道啦,主要是該站點在得知有請求時,會先判斷請求頭中的信息,如果請求頭中有Referer信息,然后根據自己的規則來判斷Referer頭信息是否符合要求,Referer 信息是請求該圖片的來源地址。


    瀏覽器中的請求頭信息:

    (1)正常使用百度貼吧查看圖片的請求頭信息

    (2)我的代碼的頭信息


    相信讀者看到這,也就明白了,為什么我的代碼不能訪問到圖片,而是顯示一張警告盜鏈圖片,因為我們的Referer頭信息和百度貼吧的不同,當我的請求發出去時,該站點查看Referer頭信息,一看來源不是本站,就重定向到另外一張圖片了。

    給自己的站點配置圖片防盜鏈:
    (1)在web服務器中開啟mod_rewrite模塊

    #LoadModule rewrite_module modules/mod_rewrite.so,//將前面的#給去掉,然后重新啟動服務器

    (2)在需要防盜的網站或目錄中,寫.htaccess文件,并指定防盜鏈規則

    步驟:
    新建一個.htaccess文件,在windows中使用另存為的方式來新建此文件
    查找手冊,在.htaccess文件中利用正則判斷



    指定規則:
    如果是圖片資源且referer頭信息是來自于本站,則通過


    重寫規則如下:
    假定服務器是localhost規則的意思是,如果請求的是圖片資源,但是請求來源不是本站的話,就重定向到當前目錄的一張no.png的圖片上

    RewriteEngine On
    RewriteCond %{script_FILENAME} .*.(jpg|jpeg|png|gif) [NC]
    RewriteCond %{HTTP_REFERER} !localhost [NC]
    RewriteRule .* no.png
    來自localhost的訪問:

    來自于其他站點的訪問:

    至此,關于防盜鏈的知識我們學完了,但是不急,既然是一個請求頭,當然是可以偽造的,下面我們來說一下反防盜鏈的規則。


    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"
    
    ");
    file_put_contents('./toutupian.jpg'substr($content4));
    echo "ok";
    ?>
    下面的代碼是從一個配置了圖片防盜鏈的站點下載一張圖片。

    不加Referer頭信息下載的結果:


    加Referer頭信息下載的結果:

    相應大家看到這,應該能看出來如何反防盜鏈吧,其實就是加上一個Referer頭信息,那么,每個站點的Referer頭信息從哪里找呢?這個應該抓包分析就可以得出來了!

    <?php
    /**
     * Http請求類
     * @author webbc
     */
    class Http{
      const CRTF = "
    ";
      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->headerarray('')$this->bodyarray(''));
        $req = implode(self::CRTF$reqestArr);
        //print_r($req);die;
        fwrite($this->fh$req);//寫入信息
        //讀取
        while(!feof($this->fh)){
          $this->response .= fread($this->fh1024);
        }
      }
      //發送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($str05);
      $email = substr($str510).'@qq.com';
      $content = substr($str10);
      $message = "發表";
      $http->post(array('username'=>$username'email'=>$email'content'=>$content'message'=>$message));
      //sleep(0.1);
    }*/
    ?>


    到此這篇關于基于PHP做個圖片防盜鏈的文章就介紹到這了,謝謝大家的支持!