您可以检查推荐网站,看看它是否是您自己的.htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST}@@%{HTTP_REFERER} !^([^@]*)@@http?://\1/.*
RewriteRule .*\.pdf [NC,F]
这将因禁止指令(403 错误)而失败。NC 是不区分大小写的。服务器需要配置为在发生 403 错误时显示某些内容。
在 PHP
此外,您可以使用允许下载的动态页面检查此类事情。以下是如何使用 PHP 执行此操作的示例:
<a href='/download.php?f=myfile&fd=mypath'>Download my PDF</a>
出于安全原因,我们.pdf
取消了链接中的名称。您可以执行类似base64_encode
名称的操作,但这不会阻止知识渊博的攻击者尝试利用您的系统。f
变量是文件名(前期),“fd”是文件夹(或路径)。
示例目录可以包括pdfs
or resources/pdf
。
它不能以 . 开头或结尾/
。我们不允许在路径或文件名中使用句点,因此有人不能做类似pdf/../../..
.
代码download.php
<?php
if((preg_match('!^[A-Za-z0-9_-]+$!',$_GET['f']))&&(preg_match('!^[^/][/A-Za-z0-9_-]+[^/]$!',$_GET['fd']))){
//we're hard-coding the line so someone can't spoof something like .htaccess
$tempPath = $_GET['fd'];
$tempFilename = $_SERVER['DOCUMENT_ROOT'].'/'.$tempPath.'/'.$_GET['f'].'.pdf';
//Make sure it's a real file
if(is_file($tempFilename)){
$referrer = $_SERVER['HTTP_REFERER'];
$serverName = $_SERVER['SERVER_NAME'];
//check the referrer
if(strpos($referrer, $serverName)){
$new_filename = $_GET['f'].'.pdf';
// We'll be outputting a PDF
header('Content-type: application/pdf');
// It will be called downloaded.pdf
$hString = 'Content-Disposition: attachment; filename="'.$new_filename.'"';
header($hString);
// The PDF source is in original.pdf
readfile($tempFilename);
} else {
//offsite link
header('Location: /download_policy.php');
exit();
}
} else {
//maybe an old file? Give them the homepage
header('Location: /');
exit();
}
} else {
//hacking attempt
header('Location: /');
exit();
}
?>