[英]Prevent direct PDF file access from URL
Redirected from here , I need to prevent access of PDF
(or any other) file types, when someone access it using direct URL. 从此处重定向,当有人使用直接URL访问
PDF
(或任何其他)文件类型时,我需要阻止对其进行访问。
The Problem 问题
Say you had a PDF file that you'd like visitors on your own site to download. 假设您有一个PDF文件,希望访问者可以在自己的网站上下载。
However, if someone were to copy this link and call it from a browser window directly, or if they were to post the link to you PDF on another website then the document shall not be accessible. 但是,如果有人要复制此链接并直接从浏览器窗口中调用它,或者如果他们要将该链接发布到另一个网站上的PDF,则该文档将不可访问。 By default it is.
默认情况下是。
I am successful in hiding the pdf file path in address bar and the url formed is http://localhost/myproject/web/viewer.php?id=11&name=sample.pdf
, but in console one can see the complete path like http://localhost/myproject/document/11/sample.pdf
. 我成功隐藏了地址栏中的pdf文件路径,并且形成的url为
http://localhost/myproject/web/viewer.php?id=11&name=sample.pdf
,但是在控制台中可以看到完整的路径,例如http://localhost/myproject/document/11/sample.pdf
。
This code redirects successfully on second time page reload, but not when I select Open in new tab
from console. 此代码在第二次页面重新加载时成功重定向,但是当我从控制台中选择“
Open in new tab
时,重定向失败。
RewriteCond %{HTTP_REFERER} !^http://(www\.)?localhost [NC]
RewriteCond %{REQUEST_URI} !hotlink\.(pdf) [NC]
RewriteRule .*\.(pdf)$ http://google.com/ [NC]
Used below code in .htaccess
to prevent access 在
.htaccess
以下代码中使用以防止访问
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://(www\.)?localhost [NC]
RewriteCond %{HTTP_REFERER} !^http://(www\.)?localhost.*$ [NC]
RewriteRule \.(pdf)$ - [F]
Somehow it does not work accurately. 不知何故,它无法正常工作。 It shows file forbidden message only if I hit
CTRL+SHIFT+R
key otherwise using the file URL I can still access it. 仅当我按
CTRL+SHIFT+R
键时,它显示文件禁止消息,否则使用文件URL仍然可以访问它。
I want to prevent the pdf file showing as http://localhost/myproject/document/11/sample.pdf
directly in the url bar 我想防止直接在网址栏中显示为
http://localhost/myproject/document/11/sample.pdf
的pdf文件
If you have access to a database, maybe you could store the file/s in a blob field. 如果您有权访问数据库,则可以将文件存储在Blob字段中。 This way you can easily write code to protect the file how you like.
这样,您可以轻松编写代码以保护自己喜欢的文件。
You can store PDF files not in webroot
folder and return it content use PHP. 您可以将PDF文件不存储在
webroot
文件夹中,并使用PHP返回其内容。 For example for user URL will be like: 例如,用户URL将类似于:
http://localhost/myproject/document.php?hash=some_hash ` http://localhost/myproject/document.php?hash = some_hash`
and on file document.php
you will get document hash from $_GET
parametr use file_get_contents
function. 在文件
document.php
您将使用file_get_contents
函数从$_GET
参数获取哈希值。 Also in this case you can allow access only once 同样在这种情况下,您只能允许一次访问
I hope it will help 希望对您有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.