Добавьте предотвращение хотлинков в этот код загрузки php - PullRequest
0 голосов
/ 16 февраля 2012

Как я могу добавить предотвращение хотлинков к следующему коду только для доступа * .mydomain.com через php?Где я могу добавить это?

<?php

$dir = 'folder';

$file = $_GET['name'];

// local file that should be send to the client
$local_file = $dir.'/'.$file;

// filename that the user gets as default
$download_file = 'video.mp4';

// set the download rate limit (=> 20,5 kb/s)
$download_rate = 200;

if(file_exists($local_file) && is_file($local_file)) {

// send headers
header('Cache-control: private');
header('Content-Type: application/octet-stream');
header('Content-Length: '.filesize($local_file));
header('Content-Disposition: filename='.$download_file);

// flush content
flush();

// open file stream
$file = fopen($local_file, "r");

while (!feof($file)) {

    // send the current file part to the browser
    print fread($file, round($download_rate * 1024));

    // flush the content to the browser
    flush();

    // sleep one second
    sleep(1);
}

// close file stream
fclose($file);


}
else {
    die('Error: File '.$local_file.' does not exist!');
}

?>

Я знаю, что это должно быть что-то вроде

define('HOTLINK_PROTECTION',TRUE); // enable hotlinking?  true/false
define('HOTLINK_PAGE_URL','http://www.mydomain.com/images/hotlink.jpg'); // Hotlink URL
$allowed_domains="*.mydomain.com, www.mydomain.com";

#checks the referer of the script
function getReferer() { preg_match('@^(?:http://)?([^/]+)@i',$_SERVER['HTTP_REFERER'], $match); return $match[1]; }

#checks if referer domain is okay
function hotlink_check() {
global $allowed_domains; $allowed_domains.=','.$_SERVER['HTTP_HOST'];
$domains=explode(',',str_replace(' ','',$allowed_domains));
$referer=getReferer(); $site=array();
foreach ($domains as $value) { $site[]='^'.str_replace('*','([0-9a-zA-Z]|\-|\_)+',str_replace('.','\.',$value)).'$'; }
foreach ($site as $pattern) { if(eregi($pattern,$referer)) $MATCH=TRUE; if($MATCH==TRUE) break; }
if($MATCH==TRUE) return TRUE; else return FALSE;
}

define('HOTLINK_PASS',hotlink_check());
if(HOTLINK_PROTECTION&&!HOTLINK_PASS&&$_SERVER['QUERY_STRING']!='admin') { header('HTTP/1.1 403 Forbidden'); header('Location: '.HOTLINK_PAGE_URL); die(); }

Но где я могу это реализовать?Как я могу это сделать?

--- Edit ---

Я сделал это, но он не работает с Mozilla Firefox ... С Firefox он просто идет прямо к горячей ссылкеimage.

Я тестировал его с Chrome, Internet Explorer, Safari и Opera, и единственным, кто привел меня к изображению с горячей ссылкой, был Firefox, я, должно быть, здесь что-то делаю не так.Вот код:

<?php

define('HOTLINK_PROTECTION',TRUE); // enable hotlinking?  true/false
define('HOTLINK_PAGE_URL','http://www.site.com/images/hotlink.jpg'); // Hotlink URL
$allowed_domains="*.site.com, www.site.com";

#checks the referer of the script
function getReferer() { preg_match('@^(?:http://)?([^/]+)@i',$_SERVER['HTTP_REFERER'], $match); return $match[1]; }

#checks if referer domain is okay
function hotlink_check() {
global $allowed_domains; $allowed_domains.=','.$_SERVER['HTTP_HOST'];
$domains=explode(',',str_replace(' ','',$allowed_domains));
$referer=getReferer(); $site=array();
foreach ($domains as $value) { $site[]='^'.str_replace('*','([0-9a-zA-Z]|\-|\_)+',str_replace('.','\.',$value)).'$'; }
foreach ($site as $pattern) { if(eregi($pattern,$referer)) $MATCH=TRUE; if($MATCH==TRUE) break; }
if($MATCH==TRUE) return TRUE; else return FALSE;
}

define('HOTLINK_PASS',hotlink_check());
if(HOTLINK_PROTECTION&&!HOTLINK_PASS) { header('HTTP/1.1 403 Forbidden'); header('Location: '.HOTLINK_PAGE_URL); die(); }

$dir = 'directory';

$video = $_GET['name'];

// local file that should be send to the client
$local_file = $dir.'/'.$video;

// filename that the user gets as default
$download_file = 'video.mp4';

// set the download rate limit (=> 200 kb/s)
$download_rate = 200;

if(file_exists($local_file) && is_file($local_file)) {

// send headers
header('Cache-control: private');
header('Content-Type: application/octet-stream');
header('Content-Length: '.filesize($local_file));
header('Content-Disposition: filename='.$download_file);

// flush content
flush();

// open file stream
$file = fopen($local_file, "r");

while (!feof($file)) {

    // send the current file part to the browser
    set_time_limit(0); 
    print fread($file, round($download_rate * 1024));

    // flush the content to the browser
    flush();

    // sleep one second
    sleep(1);
}

// close file stream
fclose($file);


}
else {
    die('Error: File '.$local_file.' does not exist!');
}

?>

1 Ответ

0 голосов
/ 03 июля 2013

У меня была такая же проблема

, что я использовал полное доменное имя в URL-адресе файла вместо относительного пути ..

    //download image now 
$file_name = "test.jpg";//$_GET['f'];
$file_url = "http://www.example.com/yfolder/". $file_name; //WRONG
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary"); 
header('Content-Type: image/jpg');    
header("Content-disposition: attachment; filename=\"".$file_name."\""); 

readfile($file_url);

правильный код

//download image now 
$file_name = "test.jpg";//$_GET['f'];
$file_url = "yfolder/". $file_name; //i removed my domain and it worked,  i managed to download the actual image instead of the hotlinked image
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary"); 
header('Content-Type: image/jpg');    
header("Content-disposition: attachment; filename=\"".$file_name."\""); 

readfile($file_url);
...