首页 > 解决方案 > 如何隐藏文件 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 文件来限制这种下载!

标签: php

解决方案


我刚刚找到了非常简单的方法,我使用此代码来阻止对特定文件类型的直接访问只需将其添加到您的 .htaccess 文件中:

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?example.com [NC]
RewriteRule \.(pdf|zip|txt|pcb|bin|rar|7z|rom)$ - [NC,F,L]

推荐阅读