donglian6625 2015-01-13 11:06
浏览 38
已采纳

防止共享下载URL

Problem

When I offer a download to a user, I don't want them to be able to copy the URL and share it with others. To go even further: I would like to give out the download only once. If they access the URL a second time, I'd like to throw a 404.

Question

How can I prevent a user from accessing the download URL a second time and is this method full proof.

Additional information

I currently serve my file as follows:

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');

I've also added a table to my mysql database

+----+------+-------+------+
| downloads                |
+----+------+-------+------+
| id | file | token | flag |
+----+------+-------+------+
  • 写回答

3条回答 默认 最新

  • douqiang1851 2015-01-13 11:13
    关注

    You would need to use a download script which redirects to the file. The script would need to verify the url via a unique token (stored in a database) before redirecting to the file. The script could record a download has been made and set a flag in the database. Check the flag to prevent future downloads.

    EDIT: Here's an Example (with pseudocode):

    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");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)