Foros del Web » Programando para Internet » PHP »

Proteger Links De Descarga

Estas en el tema de Proteger Links De Descarga en el foro de PHP en Foros del Web. BUENO la idea es EVITAR que algunos PILLOS usen los links de descarga de mi web.. (ficheros alojados en mi hosting) sea cogidos por otras ...
  #1 (permalink)  
Antiguo 21/04/2006, 15:08
Avatar de rafak20  
Fecha de Ingreso: febrero-2006
Ubicación: Colombia
Mensajes: 265
Antigüedad: 18 años, 2 meses
Puntos: 3
Exclamación Proteger Links De Descarga

BUENO la idea es EVITAR que algunos PILLOS usen los links de descarga de mi web.. (ficheros alojados en mi hosting) sea cogidos por otras personas a traves de su gestor de descargas o guardando en destino con el boton derecho del mouse.

les agradeceria si me dicen como es la vuelta...
__________________
Estampados Villavicencio
  #2 (permalink)  
Antiguo 22/04/2006, 23:01
Avatar de uamistad  
Fecha de Ingreso: diciembre-2004
Ubicación: Cd. de México
Mensajes: 1.395
Antigüedad: 19 años, 4 meses
Puntos: 1
Quién/quienes podrían descargar cosas. ¿Tú nada más?
__________________
"Di no al Internet Explorer" -Proverbio Chino-
  #3 (permalink)  
Antiguo 02/06/2006, 12:34
Avatar de rafak20  
Fecha de Ingreso: febrero-2006
Ubicación: Colombia
Mensajes: 265
Antigüedad: 18 años, 2 meses
Puntos: 3
Exclamación

la idea es bloquear la descarga para usuarios no registrados
__________________
Estampados Villavicencio
  #4 (permalink)  
Antiguo 02/06/2006, 16:48
 
Fecha de Ingreso: septiembre-2002
Ubicación: México
Mensajes: 356
Antigüedad: 21 años, 7 meses
Puntos: 0
un index de validacion

Pues yo tengo una funcion de validacion para entrar a mis paginas restringidas, y en las carpetas donde solo tengo recursos pero que no deseo permitir que accedan personas non gratas tecleando la ruta directa a la carpeta lo que hago es poner un index.php en donde solo pongo la funcion de validacion.

Asi cuando alguien intente apuntar hacia mi carpeta, lo que hace la funcion es mandarlo para fuera.

Obviamente si el usuario se sabe tambien el nombre del archivo que quiere descagar, ya es otra cuestión. Todo depende que tanto necesites.

Generalmente en esos casos lo que se hace es sacar esos recursos del área de acción del navegador, es decir, por debajo del RootDocument.

Saludos!
  #5 (permalink)  
Antiguo 02/06/2006, 20:17
Avatar de Nefertiter  
Fecha de Ingreso: enero-2003
Ubicación: Rosario
Mensajes: 1.316
Antigüedad: 21 años, 3 meses
Puntos: 9
en vez de un index puede poner un
.htaccess en el directorio, es va a impedir la visualizacion del mismo y de todos los archivos. AUN SABIENDO la ruta

.htaccess
Código PHP:
deny from all 
y bueno los archivos los decargas con un

Código PHP:
<?
$ruta 
'dir_negado/archivo.ext';
$nombre basename($ruta);
header ("Content-Disposition: attachment; filename=$nombre\n\n"); 
header("Content-Type: application/force-download");
readfile($ruta);

?>
  #6 (permalink)  
Antiguo 14/06/2006, 15:49
Avatar de Untergang  
Fecha de Ingreso: abril-2003
Ubicación: México
Mensajes: 138
Antigüedad: 21 años
Puntos: 0
Excelente!!!
__________________
<<No hay Luz sin Oscuridad>>
  #7 (permalink)  
Antiguo 14/06/2006, 16:47
Avatar de GatorV
$this->role('moderador');
 
Fecha de Ingreso: mayo-2006
Ubicación: /home/ams/
Mensajes: 38.567
Antigüedad: 17 años, 11 meses
Puntos: 2135
Yo un dia me encontre esta funcion y me ha servido perfecto:
Código PHP:
function download$file$fname="" ) {
    if( empty( 
$fname ) ) {
        
$fname explode"/"$file );
        
$fname $fname[count($fname)-1];
    }
    
    if( !
file_exists$file ) ) {
        
header("HTTP/1.1 404 Not Found");
        return;
    }
    
    
$fsize filesize($file);
    
$bufsize 20000;
    
    if(isset(
$_SERVER['HTTP_RANGE'])) { // Partial Download
        
if(preg_match("/^bytes=(\\d+)-(\\d*)$/"$_SERVER['HTTP_RANGE'], $matches)) { //parsing Range header
            
$from $matches[1];
            
$to $matches[2];
            if(empty(
$to)) {
                
$to $fsize 1;    // -1  because end byte is included
                                    //(From HTTP protocol:
                                    // 'The last-byte-pos value gives the byte-offset of the last byte in the range; that is, the byte positions specified are inclusive')
            
}
            
$content_size $to $from 1;
            
header("HTTP/1.1 206 Partial Content");
            
header("Content-Range: $from-$to/$fsize");
            
header("Content-Length: $content_size");
            
header("Content-Type: application/force-download");
            
header("Content-Disposition: attachment; filename=$fname");
            
header("Content-Transfer-Encoding: binary");
            
header("Cache-Control: private");
            
            if(
$fh fopen($fpath"rb")) {
                
fseek($fh$from);
                
$cur_pos ftell($fh);
                while(
$cur_pos !== FALSE && ftell($fh) + $bufsize $to+1) {
                    
$buffer fread($fh$bufsize);
                    print 
$buffer;
                    
$cur_pos ftell($fh);
                }
                
                
$buffer fread($fh$to+$cur_pos);
                print 
$buffer;
                
                
fclose($fh);
            } else {
                
header("HTTP/1.1 500 Internal Server Error");
                return;
            }
        } else {
            
header("HTTP/1.1 500 Internal Server Error");
            return;
        }
    } else { 
// Usual download
        
header("HTTP/1.1 200 OK");
        
header("Content-Length: $fsize");
        
header("Content-Type: application/force-download");
        
header("Content-Disposition: attachment; filename=$fname");
        
header("Content-Transfer-Encoding: binary");
        
header("Cache-Control: private");
        
        
$fh fopen($fpath"rb");
        
        while(
$buf fread($fh$bufsize))
            print 
$buf;
        
        
fclose($fh);
    }

Utilizala, como:
Código PHP:
$file_to_dl "/directorio/archivo.ext";
download$file_to_dl );
exit(); 
  #8 (permalink)  
Antiguo 15/06/2006, 02:16
Avatar de Azrael666  
Fecha de Ingreso: noviembre-2004
Mensajes: 652
Antigüedad: 19 años, 5 meses
Puntos: 1
esa funcion que hace exactamente... cualquiera puede ver el link pero solo se lo pueden bajar los registrados o algo asi?

porque estaba buscando yo algo asi para meter en mis foros, que cualquiera pueda ver el link de descarga, pero solo lo puedan descargar los registrados.. que a los otros les tire a una pagina de error, o les muestre un alert o algo asi..
__________________
ALOZORRO v5.0 (ahora en .es)
  #9 (permalink)  
Antiguo 15/06/2006, 03:22
Avatar de Nefertiter  
Fecha de Ingreso: enero-2003
Ubicación: Rosario
Mensajes: 1.316
Antigüedad: 21 años, 3 meses
Puntos: 9
esa funcion lo unico q hace es bajar un archivo, pero es cuestion de:


session_start();

$file_to_dl = "/directorio/archivo.ext";

if (isset($_SESSION['variable_session'])){


download( $file_to_dl );

}else{
echo 'te tenes q loguear flaco';
}
  #10 (permalink)  
Antiguo 15/06/2006, 03:24
Avatar de Azrael666  
Fecha de Ingreso: noviembre-2004
Mensajes: 652
Antigüedad: 19 años, 5 meses
Puntos: 1
vale.. gracias.. ya lo pruebo cuando llegue a casa
__________________
ALOZORRO v5.0 (ahora en .es)
  #11 (permalink)  
Antiguo 15/06/2006, 10:30
Avatar de mveraa  
Fecha de Ingreso: diciembre-2002
Ubicación: santiago-chilito
Mensajes: 1.931
Antigüedad: 21 años, 3 meses
Puntos: 2
hola intente hacer la funcion pero me falla algo .

cree una carpeta y puse el script y el archivo , probe un .doc y me pidio un conversor . puse un txt y me mostro esto dentro del txt que descargo.


<br />
<b>Notice</b>: Undefined variable: fpath in <b>c:\php\www\seguimientos2\prueba_donwload\prueba .php</b> on line <b>67</b><br />
<br />
<b>Warning</b>: fread(): supplied argument is not a valid stream resource in <b>c:\php\www\\prueba_donwload\prueba.php</b> on line <b>69</b><br />
<br />
<b>Warning</b>: fclose(): supplied argument is not a valid stream resource in <b>c:\php\www\\prueba_donwload\prueba.php</b> on line <b>72</b><br />

alguna idea de algo.
gracias cluster por el link.


un saludo
__________________
"Cuando se adelanta un oponente, enfréntalo y salúdalo; si intenta retroceder, déjalo seguir su camino"
  #12 (permalink)  
Antiguo 15/06/2006, 14:21
Avatar de Azrael666  
Fecha de Ingreso: noviembre-2004
Mensajes: 652
Antigüedad: 19 años, 5 meses
Puntos: 1
una cosa.. pero esta funcion no vale para cualquier link que le pases?

quiero decir.. si le meto www.google.com me daría el mismo mensaje al intentar abrirla si el usuario no esta registrado?
__________________
ALOZORRO v5.0 (ahora en .es)
  #13 (permalink)  
Antiguo 15/06/2006, 15:23
Avatar de mveraa  
Fecha de Ingreso: diciembre-2002
Ubicación: santiago-chilito
Mensajes: 1.931
Antigüedad: 21 años, 3 meses
Puntos: 2
hola no comprendo tu punto , ya que solo pruebo con la funcion para descarga un .doc que tengo en el server, aun no estoy considerando ninguna validacion de usuario.

yo solo hago esto .

<?php


function download( $file, $fname="" ) {

if( empty( $fname ) ) {
$fname = explode( "/", $file );
$fname = $fname[count($fname)-1];

}

if( !file_exists( $file ) ) {
header("HTTP/1.1 404 Not Found");
return;
}

$fsize = filesize($file);
$bufsize = 20000;

if(isset($_SERVER['HTTP_RANGE'])) { // Partial Download
if(preg_match("/^bytes=(\\d+)-(\\d*)$/", $_SERVER['HTTP_RANGE'], $matches)) { //parsing Range header
$from = $matches[1];
$to = $matches[2];
if(empty($to)) {
$to = $fsize - 1; // -1 because end byte is included
//(From HTTP protocol:
// 'The last-byte-pos value gives the byte-offset of the last byte in the range; that is, the byte positions specified are inclusive')
}
$content_size = $to - $from + 1;
header("HTTP/1.1 206 Partial Content");
header("Content-Range: $from-$to/$fsize");
header("Content-Length: $content_size");
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=$fname");
header("Content-Transfer-Encoding: binary");
header("Cache-Control: private");

if($fh = fopen($fpath, "rb")) {
fseek($fh, $from);
$cur_pos = ftell($fh);
while($cur_pos !== FALSE && ftell($fh) + $bufsize < $to+1) {
$buffer = fread($fh, $bufsize);
print $buffer;
$cur_pos = ftell($fh);
}

$buffer = fread($fh, $to+1 - $cur_pos);
print $buffer;

fclose($fh);
} else {
header("HTTP/1.1 500 Internal Server Error");
return;
}
} else {
header("HTTP/1.1 500 Internal Server Error");
return;
}
} else { // Usual download
header("HTTP/1.1 200 OK");
header("Content-Length: $fsize");
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=$fname");
header("Content-Transfer-Encoding: binary");
header("Cache-Control: private");

$fh = fopen($fpath, "rb");

while($buf = fread($fh, $bufsize))
print $buf;

fclose($fh);
}
}

//"../prueba_donwload/tu.txt"

$file_to_dl = "../prueba_donwload/tu.txt";
download( $file_to_dl );
exit();
?>
__________________
"Cuando se adelanta un oponente, enfréntalo y salúdalo; si intenta retroceder, déjalo seguir su camino"
  #14 (permalink)  
Antiguo 15/06/2006, 15:49
Avatar de mveraa  
Fecha de Ingreso: diciembre-2002
Ubicación: santiago-chilito
Mensajes: 1.931
Antigüedad: 21 años, 3 meses
Puntos: 2
encontre esta codigo que hace lo que busco y me funcion super bien.

<?PHP
$id = "llamado.doc";
$enlace = "../prueba_donwload/".$id;
header ("Content-Disposition: attachment; filename=".$id." ");
header ("Content-Type: application/octet-stream");
header ("Content-Length: ".filesize($enlace));
readfile($enlace);

?>


un saludo.
__________________
"Cuando se adelanta un oponente, enfréntalo y salúdalo; si intenta retroceder, déjalo seguir su camino"
  #15 (permalink)  
Antiguo 15/06/2006, 17:20
Avatar de Nefertiter  
Fecha de Ingreso: enero-2003
Ubicación: Rosario
Mensajes: 1.316
Antigüedad: 21 años, 3 meses
Puntos: 9
mveraa un codigo casi identico lo publique mas arriba en este post....
  #16 (permalink)  
Antiguo 13/02/2008, 07:52
Avatar de rafak20  
Fecha de Ingreso: febrero-2006
Ubicación: Colombia
Mensajes: 265
Antigüedad: 18 años, 2 meses
Puntos: 3
Re: Proteger Links De Descarga

ok. si que bien los codigos. los estoy probando y sirven . gracias a los que aportaron.
__________________
Estampados Villavicencio
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Tema Cerrado

SíEste tema le ha gustado a 1 personas




La zona horaria es GMT -6. Ahora son las 13:33.