防止共享下载网址

用户名

问题

当我向用户提供下载内容时,我不希望他们能够复制URL并与他人共享。更进一步:我只想放弃一次下载。如果他们第二次访问该URL,我想抛出404。

问题

如何防止用户第二次访问下载URL,并且此方法是充分的证明。

附加信息

我目前按以下方式提供文件:

header("Content-type: application/pdf");

// Set the name of the downloaded file here:
header("Content-Disposition: attachment;filename='example.pdf'"); 

 // Get the contents of the original file:
echo file_get_contents('example.pdf');

我还向我的mysql数据库添加了一个表

+----+------+-------+------+
| downloads                |
+----+------+-------+------+
| id | file | token | flag |
+----+------+-------+------+
本-京东

您将需要使用重定向到文件的下载脚本。在重定向到文件之前,脚本将需要通过唯一令牌(存储在数据库中)验证网址。该脚本可以记录已进行的下载并在数据库中设置一个标志。检查标志以防止将来下载。

编辑:这是一个示例(带有伪代码):

if( isset($_GET["token"]) && !empty($_GET["token"]) )
{
    //verify $_GET["token"] matches a token in the db

    //verify that the download flag has not been set

    header("Content-type: application/pdf");

    // Set the name of the downloaded file here:
    header("Content-Disposition: attachment;filename='example.pdf'"); 

    //set the downloaded flag in the database so that this file can't be downloaded again

     // Get the contents of the original file:
    echo file_get_contents('example.pdf');
}
else
    header("HTTP/1.0 404 Not Found");

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章