简体   繁体   English

如何防止直接访问文件夹中的文件?

[英]How to prevent direct access to files in folder?

A have a webpage with separated "subdir" with this structure: 有一个网页,其中的“ subdir”具有以下结构:

  • mywebpage 我的网页
    • /subdir/system/ - here are php forms files, / subdir / system / -这是php表单文件,
  • index.php index.php

The index.php calls file1.php in /system. index.php在/ system中调用file1.php。 Then other files in /system are called by "submits". 然后/ system中的其他文件被“提交”调用。 I can't write correct .htaccess to prevent calling these /system/ directly by their URLs. 我无法编写正确的.htaccess来防止直接通过其URL调用这些/ system /

I have googled and tried various .htaccess contents. 我用谷歌搜索并尝试了各种.htaccess内容。 But mostly either the "subdir" wasn't accessible at all (even not from index.php ) or the "webpage" access returned "404" . 但是大多数情况下, “ subdir”根本无法访问(甚至不能从index.php ),或者“ webpage”访问返回“ 404”

Normally a user shall open the index.php and decide what he wants to do. 通常,用户应打开index.php并决定要执行的操作。 Depending on this, he is redirected to respective /system/ .php. 依赖于此,他被重定向到相应的/ system / .php。 Any other try / eg adding https://mywebpage/subdir/system/ anyfile . 任何其他尝试/例如添加https:// mywebpage / subdir / system / anyfile shall redirect him back to index.php or any other "Error page". 应将他重定向回index.php或任何其他“错误页面”。

The typical way is to define a global constant in the entry point (like index.php) a file that is ran before anything else, then check in each file if that constant is defined 典型的方法是在入口点(如index.php)中定义一个先于其他文件运行的全局常量,然后检查每个文件是否定义了该常量

 define('SOME_CONST', true);

Then at the top of each file 然后在每个文件的顶部

if(!defined('SOME_CONST')) die ("No direct access");

Therefor accessing the file without first loading the file that defines the constant results in termination of PHP. 因此在不首先加载定义常量的文件的情况下访问文件将导致PHP终止。

This "constant" can be anything, typically I use a base path that is relative to the index.php file etc.... 这个“常量”可以是任何东西,通常我使用相对于index.php文件等的基本路径。

    define('MY_BASE_PATH', __DIR__.'/');

And so on... 等等...

A few things to keep in mind 注意事项

I should mention you should not define it in a file and include it in your other files, it wont work that way. 我应该提到您不应该在文件中定义它并将其包含在其他文件中,否则它将无法正常工作。

   //DO NOT DO THIS as IT wont WORK!!!!! - technically it never fails
  //--- in the file somefile.php ---

  //required file defines SOME_CONST
  require 'index.php'; 
  //define('SOME_CONST', true); -- defined in index.php, think of it like copy and pasting that files code at this spot.

  //will never fail, because it's defined by the file included/required above
  if(!defined('SOME_CONST')) die ("No direct access"); 

It's like putting this in your code and expecting it to fail (obviously, here it will never fail): 这就像将其放入您的代码中并期望它会失败(显然,这里永远不会失败):

  //dont do this either
  define('SOME_CONST', true);
  if(!defined('SOME_CONST')) die ("No direct access"); 

Instead do it this way: 而是这样做:

So you have to include the files from that entry point, and use something like a router etc... basic MVC. 因此,您必须包括该入口点的文件,并使用路由器等...基本的MVC。 Do this instead (vastly simplified) 改为这样做(大大简化了)

 // --- in index.php ---
define('SOME_CONST', true);

require 'somepage.php';
//if(!defined('SOME_CONST')) die ("No direct access"); included in the above require

And then 接着

//--- in somefile.php ---
if(!defined('SOME_CONST')) die ("No direct access"); //will fail if index.php is not loaded.

So if someone goes to just that somefile.php the constant is not defined. 因此,如果有人只访问somefile.php该常量未定义。 Because the index.php was not executed "BEFORE" this file.... UNLIKE if you include the index.php (in somefile.php ) before the check. 因为没有在该文件“之前”执行index.php 。...如果您在检查之前包括index.php (在somefile.php ),则不喜欢。 You Obviously cannot include it ( index.php ) after the check. 您显然不能在检查后包括它( index.php )。 So it must be ran before somefile.php and NOT when ONLY somefile.php is loaded. 因此它必须在somefile.php之前运行,而不是仅在加载somefile.php时运行。 Which is why you can't include index.php in somefile.php but instead must include somefile.php in index.php . 这就是为什么您不能在somefile.php包含index.php ,而必须在index.php包含somefile.php的原因。

Also obviously you will need more then just one page somefile.php . 同样显然,您将需要多于一页的somefile.php So in index you will need a way to direct the request to the proper page. 因此,在索引中,您将需要一种将请求定向到正确页面的方法。 This is called routing. 这称为路由。 And is a whole other topic... 这是另一个主题...

I tried to keep it as basic as possible. 我试图使它尽可能基本。 It's really quite simple. 这真的很简单。

Enjoy. 请享用。

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

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