Evitar HOTLINK con Token

  • Autor Autor zcriptz
  • Fecha de inicio Fecha de inicio
zcriptz

zcriptz

1
Ómicron
Programador
Verificación en dos pasos activada
Verificado por Whatsapp
Suscripción a IA
Esto les ayudará a evitar HOTLINK de sus enlaces de descarga directa, también sirve para reproductores de video.

Para los enlaces se genera un token con ciertos parametros (como la IP y navegador) y si alguien más entrara a ese enlace, sus datos no generarían el mismo y por lo tanto no podría descargar.

Este es el archivo que generará la descarga del archivo:
(Debe ponerse en el mismo servidor donde se encuentran los archivos)

getFile.php
PHP:
<?php 
$ip = $_SERVER['REMOTE_ADDR'];    
$key = 'KEYPERSONAL';											 //Cambiar por una "key" personal, por ejemplo: paSS@!231)cB
$token = md5($_GET['file'].$ip.$key.$_SERVER['HTTP_USER_AGENT']);  
if($token != $_GET['token']) exit;								//Si no coincide el token, no continua.
$file = $_SERVER['DOCUMENT_ROOT'].'/files/'.$_GET['file'];		//Cambiar por la dirección donde se encuentran los archivos.
if(!file_exists($file)) exit;
$fp = @fopen($file, 'rb'); 

$size = filesize($file); 
$length = $size; 
$start = 0; 
$end = $size-1; 

header("Content-Description: File Transfer");
header('Content-Type: application/octet-stream');
header("Content-Disposition: attachment; filename='".$_GET['file']."'"); 
header('Accept-Ranges: bytes'); 
if($_SERVER['HTTP_RANGE']){ 
    $c_start = $start; 
    $c_end = $end; 

    list(, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2); 
    if(strpos($range, ',') !== false){ 
        header('HTTP/1.1 416 Requested Range Not Satisfiable'); 
        header("Content-Range: bytes $start-$end/$size"); 
        exit; 
    } 
    if($range == '-'){ 
        $c_start = $size - substr($range, 1); 
    }else{ 
        $range = explode('-', $range); 
        $c_start = $range[0]; 
        $c_end = (isset($range[1]) && is_numeric($range[1])) ? $range[1] : $size; 
    } 
    $c_end = ($c_end > $end) ? $end : $c_end; 
    if($c_start > $c_end || $c_start > $size - 1 || $c_end >= $size){ 
        header('HTTP/1.1 416 Requested Range Not Satisfiable'); 
        header("Content-Range: bytes $start-$end/$size"); 
        exit; 
    } 
    $start = $c_start; 
    $end = $c_end; 
    $length = $end - $start + 1; 
    fseek($fp, $start); 
    header('HTTP/1.1 206 Partial Content'); 
} 
header("Content-Range: bytes $start-$end/$size"); 
header("Content-Length: ".$length); 

$buffer = 1024 * 8; 
while (!feof($fp) && ($p = ftell($fp)) <= $end) { 
    if($p + $buffer > $end){ 
        $buffer = $end - $p + 1; 
    } 
    set_time_limit(0); 
    echo fread($fp, $buffer); 
    flush(); 
} 

fclose($fp); 
exit() 
?>


funcFile.php
PHP:
<?php
function getFileURL($n){
	$ip = $_SERVER['HTTP_CF_CONNECTING_IP'] OR $ip = $_SERVER['REMOTE_ADDR'];  
	$key = 'KEYPERSONAL';									//Cambiar por la misma key puesta en getFile.php
	$token = md5($n.$ip.$key.$_SERVER['HTTP_USER_AGENT']);
	return "http://www.tuweb.com/getFile.php?file=$n&token=$token";
}
?>
(Este archivo lo debes llamar al PHP que necesitará la URL del archivo)

________________________________________

Ejemplo de como imprimir la URL:
PHP:
<?php
require_once('funcFile.php');								//Se incluye el archivo que tiene la función que crea la URL con Token de nuestros archivos.
$urlFile = getFileURL('nombre_de_tu_archivo.extension');	 //Se utiliza la función "getFileURL()" para generar la URL del archivo a descargar.
echo 'URL DEL ARCHIVO: '.$urlFile;	
?>

Ejemplo: http://tabunder.com/demo/file.php
 
Última edición:
Muchas Gracias, esto me Serviria Para la Descarga de mi Proyecto de pago Por reproducion y Descarga, Creo que usare Token en La Descarga :encouragement:
Ya que si uso Streaming PHP poniendo el link directo PHP en el archivo PHP para generar una url ocultado la ubicacion real del archivo me pone 100% el Servidor Lento 😕
 
Atrás
Arriba