【问题标题】:How can I password protect a binary file download?如何密码保护二进制文件下载?
【发布时间】:2013-04-21 23:43:47
【问题描述】:

我想在任意二进制文件上创建一个非常简单的 php 保护程序,用户输入密码并将文件下载到他们的计算机,但每次下载时都必须输入密码。

first answer 到问题Easy way to password-protect php page 中,include("secure.html"); 行似乎要求文件必须是可显示的 ascii,可由浏览器呈现。

有没有办法保护二进制文件,比如foo.bin,具有相同的简单性(以及类似的有限安全程度)?

【问题讨论】:

  • 我推荐在 PHP 中使用 PHPass 进行密码散列,它既简单又非常安全。 openwall.com/phpass

标签: php password-protection


【解决方案1】:

将文件夹设置为存储文件的位置以拒绝所有,然后使用 readfile 您应该能够访问它。

<?php
    if (!empty($_POST)) {
        $user = $_POST['user'];
        $pass = $_POST['pass'];

        if($user == "admin"
        && $pass == "admin")
        {
            $file = 'path/to/file';

            if (file_exists($file)) {
                header('Content-Description: File Transfer');
                header('Content-Type: application/octet-stream');
                header('Content-Disposition: attachment; filename='.basename($file));
                header('Content-Transfer-Encoding: binary');
                header('Expires: 0');
                header('Cache-Control: must-revalidate');
                header('Pragma: public');
                header('Content-Length: ' . filesize($file));
                ob_clean();
                flush();
                readfile($file);
                exit;
            }
        }
    }
?>

<form method="POST" action="">
    User <input type="TEXT" name="user"></input>
    Pass <input type="TEXT" name="pass"></input>
    <input type="submit" name="submit"></input>
</form>

【讨论】:

  • 我曾想过将 Apache 与 HTTP Auth 一起使用,但现代浏览器倾向于缓存凭据,我想强制用户在每次登录时输入密码。
  • 你不能使用如下标题:header("Cache-Control: no-store, no-cache, must-revalidate)
  • 不清楚是否所有主流浏览器都会真正遵守该服务器指令。另外,我在 cgi-bin 之外运行 PHP,这可能会进一步限制此类标头的使用。
  • 我相信您可以将问题中示例中的包含替换为文件路径的标题。请参阅我的答案中的编辑。
  • 一定要喜欢 php 文档中的旧副本+粘贴:php.net/manual/en/function.readfile.php#example-2374
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-16
  • 2018-06-07
  • 2011-11-03
  • 2014-06-22
  • 1970-01-01
  • 2015-03-18
相关资源
最近更新 更多