[英]Prevent direct access to a PDF on a PHP secured website
我用登录名创建了一个PHP网站,用户可以访问PDF文件。 如何防止未经授权的用户通过直接链接访问PDF文件?
我尝试使用.htaccess进行尝试,但是“拒绝所有来自本地主机的允许”也阻止了我登录的用户。 我尝试使用RewriteEngine,即使我删除了具有空Referer的行,它也允许所有操作:
# ultimate hotlink protection
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g?|png)$ [NC]
RewriteCond %{HTTP_REFERER} !^https?://([^.]+\.)?domain\. [NC]
RewriteRule \.(gif|jpe?g?|png)$ - [F,NC,L]
</ifModule>
我能做什么?
您可以在阻止列表中使用pdf
:
# ultimate hotlink protection
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{HTTP_REFERER} !^https?://([^.]+\.)?domain\. [NC]
RewriteRule \.(gif|jpe?g?|png|pdf)$ - [F,NC,L]
</ifModule>
但是请记住,基于HTTP_REFERER
阻止并非100%安全,因为客户端也可以发送自定义HTTP_REFERER
标头。
如果您只希望只有登录用户可以下载pdf文件:例如,在html页面中,您可以放置以下链接:
<a href:'download.php'>Download now!</a>
然后创建这样的php文件:
download.php
<?php
session_start();
if(!isset($_SESSION["IdUser"])) {
header("location:goToHell.php");
}
else {
$file="yourPath/theFile.pdf";
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.