PHP资源下载防盗链类实例代码

原文参考:http://blog.chinaunix.net/uid-11840697-id-4248564.html
分享一个PHP写的资源下载
防盗链 的类,感兴趣的朋友可以共同探讨研究。
简单的PHP防盗链处理类(重新整理编写成类文件,以便后期改进);

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值