2016-09-17 134 views
-2

我的网站包含一些下载内容。我想访问下载此文件只为登录用户。如何阻止直接下载文件

如果用户在浏览器中键入直接文件url,则在用户未登录时显示禁止页面。不使用任何CMS。 直接文件链接:localhost/files/questions/20160917070900-w2CdE9LZpE.zip

我在网上搜索,但没有找到任何好的答案。请告诉我该怎么做。

+0

看一看http://stackoverflow.com/questions/13881041/htaccess-deny-acces-to-directory? - 如果-已登录out-checked-with-php看看这可以用php来完成。 – Chris

+0

它不阻止直接文件链接..其只是重定向,如果用户没有登录的PHP文件 –

回答

0

进入文件夹中的成员创建新的文件夹中的文件,在这里将你的所有歌曲,创建新的.htaccess文件,并添加以下行:

Order Deny,Allow 
Deny from all 

进入文件夹成员建立的文件get_file.php并添加以下代码:

if(!empty($_GET['name'])) 
{ 
    // check if user is logged  
    if(is_logged()) 
    { 
    $file_name = preg_replace('#[^-\w]#', '', $_GET['name']); 
    $question_file = "{$_SERVER['DOCUMENT_ROOT']}/files/questions/{$file_name}.zip"; 
    if(file_exists($question_file)) 
    { 
    header('Cache-Control: public'); 
    header('Content-Description: File Transfer'); 
    header("Content-Disposition: attachment; filename={$question_file}"); 
    header('Content-Type: application/zip'); 
    header('Content-Transfer-Encoding: binary'); 
    readfile($question_file); 
    exit; 
    } 
    } 
} 
die("ERROR: invalid song or you don't have permissions to download it."); 

URL来获取文件:本地主机/ get_file.php名= file_name

0

把你想要在一个单独的目录来保护文件,并与下面添加一个.htaccess文件:

Order deny,allow 
Deny from all 

服务器,并且在该目录中访问文件脚本仍然可以使用,而是由URL直接访问将不会。

+0

它否认直接链接,但也否认,如果用户点击下载链接 –

+0

您的链接正在调用一个PHP脚本处理下载,检查看看是否用户是否先登录? – Chris

+0

谢谢我明白了 –