【发布时间】:2013-12-03 12:30:24
【问题描述】:
要登录网站,将 loginForm 提交 (POST) 到 login.php 脚本。
查询Mysql,如果username/md5 pw匹配,则设置几个$_SESSION变量。
使用 header('Location:secureLinks.php') 将用户重定向到安全页面。
在secureLinks.php 上,我使用if (ISSET($_SESSION)){} 检查$_SESSION 变量。
这是有效的,如果用户通过了身份验证,那么他们可以点击链接来访问文件。链接位于secureLinks.php,如下所示:http://mysite/files/link1.pdf
如果您从secureLinks.php 复制链接然后打开一个新的浏览器,您可以直接导航到该链接,而无需通过secureLinks.php。
保护 mysite/files 目录的方法是什么?这是在使用指令的 web 服务器层 apache2 上完成的吗?
谢谢
编辑 - 解决方案
apache2 虚拟服务器配置:
<Directory "/something/path/securedDirectory">
Options -Indexes
Order Deny,Allow
Deny from all
</Directory>
securedLinks.php 返回文件列表
$fileList = scandir($securedDirectory);
jQuery 循环遍历 $fileList,添加到securedLinks.php 的链接
$.each(linkObj, function(k,v){
var a = "<a href='reportLoader.php?fileName=" + v + "'>" + v + "</a><br>";
$("#reportLinks").append(a);
});
jQuery 也会停止默认点击并去除 href,然后设置下载
$(document).on("click", "#reportLinks a", function(e){
e.preventDefault();
var params = $(this).attr("href");
getPdf(params);
});
function getPdf(urlParams){
if (urlParams.length > 0){
window.location.href = urlParams;
}
}
最后,reportLoader.php 是如何流式传输文件的
session_start();
if (isset($_SESSION['myVar'])) {
$fileName = $_REQUEST['fileName'];
$fullPath = $_SERVER['DOCUMENT_ROOT']."/securedDirectory/".$fileName;
if ($fd = fopen ($fullPath, "r")) {
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);
switch ($ext) {
case "pdf":
header("Content-type: application/pdf");
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\"");
break;
case "doc":
header("Content-type: application/msword");
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\"");
break;
case "docx":
header("Content-type: application/vnd.openxmlformats-officedocument.wordprocessingml.document");
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\"");
break;
}
header("Content-length: $fsize");
header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0"); //this is incompatible with IE8
header("Content-Transfer-Encoding: Binary");
readfile("$fullPath");
fclose ($fd);
}
}
【问题讨论】: