简体   繁体   English

阻止通过HTTP直接访问文件,但允许php脚本访问

[英]Block direct access to a file over http but allow php script access

I'm loading my files (pdf, doc, flv, etc) into a buffer and serving them to my users with a script. 我正在将文件(pdf,doc,flv等)加载到缓冲区中,并通过脚本将其提供给我的用户。 I need my script to be able to access the file but not allow direct access to it. 我需要我的脚本能够访问文件,但不允许直接访问它。 Whats the best way to achieve this? 什么是实现这一目标的最佳方法? Should I be doing something with my permissions or locking out the directory with .htaccess? 我应该使用我的权限来做某事还是使用.htaccess锁定目录?

The safest way is to put the files you want kept to yourself outside of the web root directory, like Damien suggested. 最安全的方法是将想要保留的文件放到Web根目录之外,就像Damien建议的那样。 This works because the web server follows local file system privileges, not its own privileges. 之所以可行,是因为Web服务器遵循本地文件系统特权,而不是其自身的特权。

However, there are a lot of hosting companies that only give you access to the web root. 但是,有很多托管公司只允许您访问Web根目录。 To still prevent HTTP requests to the files, put them into a directory by themselves with a .htaccess file that blocks all communication. 为了仍然阻止对文件的HTTP请求,请使用阻止所有通信的.htaccess文件将它们自己放置在目录中。 For example, 例如,

Order deny,allow
Deny from all

Your web server, and therefore your server side language, will still be able to read them because the directory's local permissions allow the web server to read and execute the files. 您的Web服务器以及您的服务器端语言仍将能够读取它们,因为目录的本地权限允许Web服务器读取和执行文件。

That is how I prevented direct access from URL to my ini files. 这就是我阻止直接从URL访问ini文件的方式。 Paste the following code in .htaccess file on root. 将以下代码粘贴到根目录下的.htaccess文件中。 (no need to create extra folder) (无需创建额外的文件夹)

<Files ~ "\.ini$">
  Order allow,deny
  Deny from all
</Files>

my settings.ini file is on the root, and without this code is accessible www.mydomain.com/settings.ini 我的settings.ini文件位于根目录上,如果没有此代码,则可访问www.mydomain.com/settings.ini

in httpd.conf to block browser & wget access to include files especially say db.inc or config.inc . 在httpd.conf中阻止浏览器和wget访问包含文件,尤其是db.inc或config.inc。 Note you cannot chain file types in the directive instead create multiple file directives. 请注意,您不能在指令中链接文件类型,而不能创建多个文件指令。

<Files ~ "\.inc$">
Order allow,deny
Deny from all
</Files>

to test your config before restarting apache 在重新启动Apache之前测试您的配置

service httpd configtest

then (graceful restart) 然后(正常重启)

service httpd graceful

Are the files on the same server as the PHP script? 文件是否与PHP脚本位于同一服务器上? If so, just keep the files out of the web root and make sure your PHP script has read permissions for wherever they're stored. 如果是这样,只需将文件放在Web根目录之外,并确保您的PHP脚本无论存储在哪里都具有读取权限。

If you have access to you httpd.conf file (in ubuntu it is in the /etc/apache2 directory), you should add the same lines that you would to the .htaccess file in the specific directory. 如果您有权访问httpd.conf文件(在ubuntu中,该文件位于/ etc / apache2目录中),则应在特定目录中的.htaccess文件中添加相同的行。 That is (for example): 那是(例如):

ServerName YOURSERVERNAMEHERE
<Directory /var/www/>
AllowOverride None
order deny,allow
Options -Indexes FollowSymLinks
</Directory>

Do this for every directory that you want to control the information, and you will have one file in one spot to manage all access. 对要控制信息的每个目录执行此操作,并且一个位置将有一个文件来管理所有访问。 It the example above, I did it for the root directory, /var/www. 在上面的示例中,我对根目录/ var / www进行了此操作。

This option may not be available with outsourced hosting, especially shared hosting. 此选项可能不适用于外包托管,尤其是共享托管。 But it is a better option than adding many .htaccess files. 但这是比添加许多.htaccess文件更好的选择。

To prevent .ini files from web access put the following into apache2.conf 为了防止.ini文件通过Web访问,请将以下内容放入apache2.conf中

<Files ~ "\.ini$">
Order allow,deny
Deny from all
</Files>

How about custom module based .htaccess script (like its used in CodeIgniter)? 基于自定义模块的.htaccess脚本(如CodeIgniter中使用的脚本)怎么样? I tried and it worked good in CodeIgniter apps. 我尝试了一下,在CodeIgniter应用程序中效果很好。 Any ideas to use it on other apps? 有其他想法可以在其他应用程序上使用吗?

<IfModule authz_core_module>
    Require all denied
</IfModule>
<IfModule !authz_core_module>
    Deny from all
</IfModule>

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

相关问题 阻止直接HTTP访问PHP文件 - Block direct HTTP access to a PHP File 阻止直接访问PHP文件,仅允许本地javascript访问它 - block direct access to PHP file and only allow local javascript to access it PHP禁止直接访问文件,但仅允许文件所有者登录 - PHP Block direct access to a file over but allow to only logged in owner of file 阻止直接访问文件,但允许通过jquery加载功能进行访问 - block direct access to file but allow access through jquerys load function 允许直接访问特定的php文件 - Allow direct access for specific php file 如何阻止直接访问我的php文件 - How to block direct access to my php file PHP:如何阻止对文件的直接URL访问,但仍允许登录用户下载文件? - PHP: How can I block direct URL access to a file, but still allow it to be downloaded by logged in users? 拒绝直接访问文件夹或文件,但允许index.php访问它 - Deny direct access to a folder or file, but allow index.php to access it 有没有办法禁止直接访问文件,但允许使用IIS通过PHP脚本下载? - Is there a way to disallow direct access of file but allow download via PHP script using IIS? 允许 PHP 脚本访问文件夹中的 PDF - 但阻止直接 URL 引用 - Allow a PHP script access to PDFs in a folder - but prevent direct URL references
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM