【问题标题】:Web-hosted file authorization网络托管文件授权
【发布时间】:2012-01-24 11:39:28
【问题描述】:

我使用基于 PHP 的登录身份验证机制来允许/限制访问我网站的某些部分(文件夹 module1module2 等),但我遇到了问题限制对文件的访问。 我使用 documents 文件夹(检查下方)来托管一些可下载的文件。这些文件的链接出现在 index.php 中(托管在 root 目录中)。但是,如果由于某种原因,未经授权的用户获得了 documents 中的文件的 URL,他将能够下载它。

/
/documents/
/module1/
/module2/

PS:由于这是一个内网网站,我通过 IP 限制了对 文档 的访问,但仍然有很小的机会有人使用具有允许 IP 地址的 PC 并且他有文件。

【问题讨论】:

    标签: authentication file-security


    【解决方案1】:

    使用某种代理 PHP 脚本,它会为用户提供文件而不提供真实的源位置。

    然后用户会看到http://yourdomain.com/download.php?file=mydoc.docx

    真正的路径仍然是 /documents/userid/2342/mydoc.docx 或者你的结构是什么样的。

    然后让您的 download.php 文件通过以下方式提供文件:

    <?php
    // Validate the user here
    
    // Set document root
    $root = 'documents/userid/'.$userID.'/';
    
    // Requested file
    $file = $_GET['file'];
    
    // Validate
    if (file_exists($root . $file))
    {
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Cache-Control: private", false);
        header("Content-Type: application/force-download");
        header("Content-Disposition: attachment; filename=\"".basename($file)."\";");
        header("Content-Transfer-Encoding: binary");
        header("Content-Length: ".filesize($root . $file));
    
        ob_clean();
        flush();
        readfile($root . $file);
    }
    else { echo "File not found"; }
    ?>
    

    See more here

    【讨论】:

      猜你喜欢
      • 2014-10-26
      • 1970-01-01
      • 2015-11-19
      • 2017-12-29
      • 2014-11-05
      • 2021-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多