【问题标题】:secure a directory that hosts links保护托管链接的目录
【发布时间】: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);
    }
}

【问题讨论】:

    标签: php apache2


    【解决方案1】:

    通常,如果您想将目录设为私有,您可以使用 apache 使用 .htaccess 文件来限制目录,或者将目录的权限设置为仅 apache。这样就无法获得外部访问权限,然后当您需要文件时,apache 可以在对用户安全的特定页面上为您提供这些文件。因此,在脚本securedLinks.php 上,apache 可以访问这些文件和目录,因此可以将它们提供给用户查看。

    【讨论】:

      猜你喜欢
      • 2022-01-06
      • 1970-01-01
      • 2020-12-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-03
      • 2020-01-24
      • 2013-10-03
      相关资源
      最近更新 更多