php - 如何隐藏文件 url 以提高安全性
问题描述
在我完成了一个只让成员从页面下载文件的小脚本后,我发现如果人们查看页面源代码并从那里下载文件,他们可以获得文件链接。
所以基本上代码不限制会员下载文件,它甚至从地址栏中隐藏文件URL,但是当有人右键单击页面并查看页面源代码时,它可以获得文件真实URL,并且他们可以以某种方式下载它没有任何限制!
像这个例子:
https://www.example.com/files/J730F/GalaxyJ72017SM-J730FCOVER.pdf
这是我的代码:
if (!(isset($_GET['username']) && !empty($_GET['username']))){
echo 'Only a member of this website can download this file. However, no username was specified in this download. Sorry for inconvenience.';
die;
}
$dl_username = $this->decrypt($_GET['username']);
if (gator::getUser($dl_username) == false){
echo 'Only a member of this website can download this file. However, the username provided does not exist in the database. Sorry for inconvenience.';
die;
}
有没有办法隐藏这件事?例如使用 .htaccess 文件来限制这种下载!
解决方案
我刚刚找到了非常简单的方法,我使用此代码来阻止对特定文件类型的直接访问只需将其添加到您的 .htaccess 文件中:
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?example.com [NC]
RewriteRule \.(pdf|zip|txt|pcb|bin|rar|7z|rom)$ - [NC,F,L]
推荐阅读
- java - 在Java中通过分隔线分割文件
- reactjs - 如何在 JSX / React 中键入检查不可渲染的对象
- python - Python:为循环的每次迭代增加符号函数的数量
- xml - XMLSyntaxError:需要开始标记,未找到“<”,第 1 行,第 1 列
- snmp - SNMPd 向外部打开除 161 和 162 之外的“随机”UDP 端口
- android - 从外部活动中隐藏 RecyclerView 中的按钮
- testing - 在 fixture.before 钩子中添加 Login 方法会在 Testcafe 中出现错误
- xslt - xslt 的 XPath 表达式
- jquery - JQuery中按名称选择多个按钮
- three.js - 为什么我的物体会“沉入地下”?