PHP: Stream PDF (make PDF accessible only from webpage)

馋奶兔 提交于 2020-01-07 09:18:11

问题


I have a site built in PHP and we have some PHP pages that require logging in that produce links to PDFs. The problem is that those PDFs (which were copied out to Amazon Cloudfront) were indexed and available for searching.

What is the easiest way to stream an existing PDF to the browser, making it so that the user has to login to be able to see the document? I'm hoping that there is some simple Header code or something where I can say "load this file on the server that's not accessible on the web and output it as PDF".

Any thoughts/suggestions?

Thanks!


回答1:


You can use htaccess (or similar) to redirect any requests for a .pdf document to a PHP script, passing the requested file name. The script can then validate the log-in credentials, and if the user is logged in it can then send PDF headers, fetch the PDF document (file_get_contents) and output the code.




回答2:


You can either block access to files (as mentioned in the other answer) or (more cleverly, IMHO) you can pass the file through to the browser after checking credentials (or doing pretty much anything) in PHP. There are code examples and a discussion here: http://bytes.com/topic/php/answers/159354-pass-through-any-file



来源:https://stackoverflow.com/questions/10453099/php-stream-pdf-make-pdf-accessible-only-from-webpage

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!