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

这篇文章主要介绍了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. $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(!emptyempty($$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://phpfensi.com';
  114. // 许可来路域名列表
  115. $allowUrl = array(
  116. 'jb51.net',
  117. );
  118. $BurglarDow = new BurglarDow($dowUrl,$Location,$allowUrl);
  119. $BurglarDow -> dow();