当前位置:首页 > PHP教程 > php类库 > 列表

PHP写的资源下载防盗链类分享

发布:smiling 来源: PHP粉丝网  添加日期:2020-12-14 16:57:48 浏览: 评论:0 

这篇文章主要介绍了PHP写的资源下载防盗链类分享,需要的朋友可以参考下,这几天在写一个PHP防盗链外部资源下载处理函数,昨天晚上刚完成编写,中间遇到了些问题,这里就不详述了;

以下是自写的简单的PHP防盗链处理类(重新整理编写成类文件,以便后期改进);代码如下:

  1. <?php 
  2. /** 
  3.  * 
  4.  * 防盗链外部资源下载处理类 
  5.  * 
  6.  * @link   http://phpfensi.com 
  7.  * 
  8.  */ 
  9. class BurglarDow{ 
  10.  /** 
  11.      * 初始许可下载状态 
  12.      * @var    allow 
  13.      * @access private 
  14.      */ 
  15.  private $allow      =  false; 
  16.  /** 
  17.      * 初始下载地址 
  18.      * @var    dowUrl 
  19.      * @access private 
  20.      */ 
  21.  private $dowUrl     =  null; 
  22.  /** 
  23.      * 初始来路域名 
  24.      * @var    RemoteUrl 
  25.      * @access private 
  26.      */ 
  27.  private $RemoteUrl  =  null; 
  28.  /** 
  29.      * 初始许可资源取用域名列表 
  30.      * @var    allowUrl 
  31.      * @access private 
  32.      */ 
  33.  private $allowUrl   =  array(); 
  34.  /** 
  35.      * 初始转跳地址 
  36.      * @var    Location 
  37.      * @access private 
  38.      */ 
  39.  private $Location   =  null; 
  40.  public function __construct($dowUrl,$Location,array $allowUrl){ 
  41.   // 初始下载地址 
  42.   $this->dowUrl   = $dowUrl
  43.   // 初始许可资源取用域名列表 
  44.   $this->allowUrl = $allowUrl
  45.   // 初始转跳地址 
  46.   $this->Location = $Location
  47.  
  48.   $this->RemoteUrl = @parse_url($_SERVER['HTTP_REFERER']);                                                      // 获取来路域名 
  49.   if(!is_array($this->RemoteUrl)) 
  50.    header("HTTP/1.1 301 Moved Permanently"); 
  51.    header("Location: ".$this->Location); 
  52.  
  53.   if(isset($this->RemoteUrl['host'])){ 
  54.    if(in_array($this->RemoteUrl['host'],$this->allowUrl)){                                                   // 判断是否来至许可域名 
  55.     $this->allow  = true;                                                                                 // 下载许可状态为:真 
  56.    } 
  57.   } 
  58.   unset($this->allowUrl,$this->RemoteUrl);                                                                      // 释放内存变量 
  59.  } 
  60.  
  61.  /** 
  62.   * 防盗链资源下载 
  63.   * @access public 
  64.   * @return mixed 
  65.   */ 
  66.  public function dow(){ 
  67.   $FileInfo = get_headers($this->dowUrl,1);                                                                     // 获取远程文件头部信息 
  68.  
  69.   if(true === $this->allow){                                                                                    // 判断是否许可下载资源 
  70.    //判断配置文件是否存在 
  71.    if(is_file('Config.ini')){ 
  72.     $FileCon = parse_ini_file('Config.ini'); 
  73.    }else
  74.     $FileName   =  basename($FileInfo['Content-Location']); 
  75.     $FileConStr = "FileName  = {$FileName}\r\nFileUrl   = {$FileInfo['Content-Location']}\r\nFileSize   = {$FileInfo['Content-Length']}"
  76.     $handle = fopen ('Config.ini'"wb");                                                                 // Config.ini文件不存在则创建文件 
  77.     if (fwrite ($handle$FileConStr) == FALSE) {                                                         // 数据写入文件 
  78.      echo "File creation failed ..."
  79.     } 
  80.     fclose ($handle);                                                                                     // 关闭一个已打开的文件指针 
  81.     $FileCon = parse_ini_file('Config.ini'); 
  82.    } 
  83.    if(!emptyempty($$this->dowUrl)){ 
  84.     $fp = @fopen($$this->dowUrl, "rb");                                                                   // 二进制模式读取文件 
  85.     if (!$fp
  86.       exit("Download a mistake.\n\n"); 
  87.  
  88.     // 输出远程资源 
  89.     header("Content-type:text/html;charset=utf-8"); 
  90.     header('Content-Description: File Transfer'); 
  91.     header('Content-Type: application/octet-stream'); 
  92.     header('Content-Disposition: attachment; filename='.$FileCon['FileName']); 
  93.     header("Accept-Ranges: bytes"); 
  94.     header('Content-Transfer-Encoding: binary'); 
  95.     header('Expires: 0'); 
  96.     header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); 
  97.     header('Pragma: public'); 
  98.     header('Content-Length: '.$FileCon['FileSize']); 
  99.     while (!feof($fp)){ 
  100.      set_time_limit(0);                                                                                 // 设置文件最长执行时间 
  101.      echo fread($fp, 1024);                                                                             // 输出文件 
  102.      flush();                                                                                           // 输出缓冲 
  103.      ob_flush();                                                                                        // 输出缓冲区中的内容 
  104.     } 
  105.     fclose($fp); 
  106.    }else
  107.     header("HTTP/1.1 404 Not Found"); 
  108.    } 
  109.   }else
  110.    header("HTTP/1.1 301 Moved Permanently"); 
  111.    header("Location: ".$this->Location); 
  112.   } 
  113.  } 
  114. // 远程资源地址 
  115. $dowUrl = 'http://dldir1.qq.com/qqfile/qq/QQ5.1/10055/QQ5.1.exe'
  116. // 转跳地址 
  117. $Location = 'http://phpfensi.com'
  118. // 许可来路域名列表 
  119. $allowUrl = array
  120.  'jb51.net'
  121. ); 
  122. $BurglarDow = new BurglarDow($dowUrl,$Location,$allowUrl); 
  123. $BurglarDow -> dow(); 

Tags: PHP资源下载类 PHP防盗链类

分享到: