我正在尝试弄清楚如何在 PHP 中保护数字下载。只需要一些一般性的指导,这样我就可以开始我的研究了。我似乎找不到任何有用的东西。
我想让我的用户可以下载文件,但不希望他们能够直接访问下载文件夹。此外,我希望下载链接仅在设定的时间段或单次下载中可用。
有人能给我指出正确的方向吗?
最佳答案
最好的方法是在检查后将下载管理委托(delegate)给 apache 的 mod
x_sendfile
https://tn123.org/mod_xsendfile/
用法:
<?php
...
if ($user->isLoggedIn())
{
header("X-Sendfile: $path_to_somefile");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$somefile\"");
exit;
}
?>
<h1>Permission denied</h1>
<p>Login first!</p>
基本上,当您发送 header X-Sendfile 时,mod 会拦截文件并为您管理下载(文件可以在您想要的虚拟主机之外的任何时间找到)。
否则你可以只实现一个简单的文件 download.php 获取文件的 id 并在登录检查后用 readfile
打印内容
关于PHP - 保护数字下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5412328/