简体   繁体   English

防止直接URL访问文件

[英]Prevent direct url access to files

Background info: 背景资料:

I am working on a website which will provide image and video content via a subscription service. 我正在建立一个网站,通过订阅服务提供图像和视频内容。 That is, users should ONLY have access to the image and video content so long as they are logged in successfully. 也就是说,只要用户成功登录,用户就应该只能访问图像和视频内容。 (Note: the log in system uses a combination of MySQL DB - to store the username and password - and php to create new user sessions / authentication etc.) (注意:登录系统使用MySQL DB的组合 - 存储用户名和密码 - 和php创建新的用户会话/身份验证等)

The problem: 问题:

How do I stop a user (logged in or not) from directly accessing the image and video files? 如何阻止用户(登录或不登录)直接访问图像和视频文件? For example, a user who is not logged in could access the file directly as follows: www.domain.com/testvideo.mp4 - this would render the video content in the browser for them to watch or share with others. 例如,未登录的用户可以直接访问该文件,如下所示:www.domain.com/testvideo.mp4 - 这将在浏览器中呈现视频内容,供他们观看或与他人共享。 (NOTE: I still need to be able to use / display the image and video files on-site via HTML, CSS, PHP etc) (注意:我仍然需要能够通过HTML,CSS,PHP等在现场使用/显示图像和视频文件)

I have tried several .htaccess solutions (including: RewriteCond/RewriteRule & .htpassword) which have successfully prevented direct access BUT have prevented the ability to use the files on-site via HTML, CSS, PHP etc. 我已经尝试了几个成功阻止直接访问的.htaccess解决方案(包括:RewriteCond / RewriteRule和.htpassword)但是已经阻止了通过HTML,CSS,PHP等在现场使用文件的能力。

I was thinking that this must be a very common problem and if so, what the best way to resolve it was? 我认为这一定是一个非常普遍的问题,如果是这样,解决问题的最佳方法是什么?

It is a pretty common problem with a pretty common solution. 这是一个非常常见的问题,有一个很常见的解决方案。 In order to force access control you have to invoke a PHP script before serving the file and verify the credentials. 为了强制访问控制,您必须在提供文件之前调用PHP脚本并验证凭据。 Then, if the credentials are valid, serve the actual file. 然后,如果凭据有效,请提供实际文件。

You may be tempted to serve the file directly from PHP script using something like readfile . 您可能想要使用readfile东西直接从PHP脚本提供文件。 This is going to kill your server performance and break download resuming for the client. 这将破坏您的服务器性能并破坏客户端的下载恢复。

Luckily there is a solution, when you can hand over the actual file serving back to the web-server. 幸运的是,当您可以将实际文件服务交还给Web服务器时,有一个解决方案。

This works as following: 其工作原理如下:

  1. The web-server receives the request to /file.mp4 . Web服务器接收对/file.mp4的请求。
  2. According to the rewrite rules you've set up it directs it to your PHP script /serve.php instead. 根据您设置的重写规则,它会将其指向您的PHP脚本/serve.php
  3. Your script verifies the credentials, eg something from the session or cookies. 您的脚本会验证凭据,例如来自会话或Cookie的内容。
  4. If the credentials are valid, the script issues specially crafted header. 如果凭据有效,则脚本会发出特制的标头。 It tells the web-server to actually serve the static file. 它告诉Web服务器实际提供静态文件。 If not, you may as well output a 403 HTTP code. 如果没有,您也可以输出403 HTTP代码。

The example script can be something like: 示例脚本可以是:

$file = '/tmp/file.mp4'; // it is in your best interest to make this file  inaccessible for a direct download
header('X-Sendfile: ' . $file);
header('Content-Type: ' . contentType($file));
header('Content-Disposition: inline;');

In order for this to work you'll have to have mod_xsendfile ( https://tn123.org/mod_xsendfile/ ) installed on your Apache, which is probably already the case for your hoster. 为了使其工作,您必须在Apache上安装mod_xsendfile( https://tn123.org/mod_xsendfile/ ),这可能已经是您的主机的情况。 You'll also have to drop in some lines to configure it and setup a proper rewrite. 您还需要删除一些行来配置它并设置正确的重写。

You can fine a lot of stuff on Google by issuing "mod_xsendfile php", which might also help a great deal. 您可以通过发布“mod_xsendfile php”来解决Google上的大量问题,这可能也会有很大帮助。

Hope that makes sense! 希望有道理!

You cannot avoid that as long as your files are publicly available. 只要您的文件是公开可用的,您就无法避免这种情况。

The most common way is to not serve the files directly, but to serve them through php so that you can check the users access before you serve the file. 最常见的方法是不直接提供文件,而是通过php提供服务,以便在提供文件之前检查用户访问权限。 And the files can then reside anywhere on the server where the web-server user (www, apache, etc.) has access but the visitor hasn't. 然后文件可以驻留在服务器上的任何位置,其中Web服务器用户(www,apache等)可以访问但访问者没有访问。

Check the examples in the php manual on readfile and header to see how you can serve a file through php. 查看关于readfileheader的php手册中的示例,了解如何通过php提供文件。 You will find lots of examples here on SO as well. 你也会在SO上找到很多例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM