简体   繁体   English

如何保护我的服务器上的php文件不被请求

[英]How to protect my php files on the server from being requested

I'm very new to php and web , now I'm learning about oop in php and how to divide my program into classes each in .php file. 我是php和web的新手,现在我正在学习php中的oop以及如何将我的程序分成每个.php文件中的类。 before now all I know about php program, that I may have these files into my root folder 之前所有我都知道php程序,我可能将这些文件放到我的根文件夹中

  1. home.php home.php
  2. about.php about.php
  3. products.php products.php
  4. contact.php contact.php

So, whenever the client requests any of that in the browser 因此,每当客户端在浏览器中请求任何内容时

http://www.example.com/home.php
http://www.example.com/about.php
http://www.example.com/products.php
http://www.example.com/contact.php

No problem, the files will output the proper page to the client. 没问题,文件会将正确的页面输出到客户端。

Now, I have a problem. 现在,我有一个问题。 I also have files like these in the root folder 我在根文件夹中也有这样的文件

  1. class1.php class1.php
  2. class2.php class2.php
  3. resources/myFunctions.php 资源/ myFunctions.php
  4. resources/otherFunctions.php 资源/ otherFunctions.php

how to prevent the user from requesting these files by typing something like this in the browser ? 如何通过在浏览器中输入这样的内容来阻止用户请求这些文件?

 http://www.example.com/resources/myFunctions.php

The ways that I have been thinking of is by adding this line on top of every file of them exit; 我一直在考虑的方法是在它们的每个文件的顶部添加这一行exit;

Or, I know there is something called .htaccess that is an Apache configuration file that effect the way that the Apache works. 或者,我知道有一个名为.htaccess的东西,它是一个影响Apache工作方式的Apache配置文件。

What do real life applications do to solve this problem ? 现实生活应用程序如何解决这个问题?

You would indeed use whatever server side configuration options are available to you. 您确实可以使用任何可用的服务器端配置选项。

Depending on how your hosting is set up you could either modify the include path for PHP ( http://php.net/manual/en/ini.core.php#ini.include-path ) or restricting the various documents/directories to specific hosts/subnets/no access in the Apache site configuration ( https://httpd.apache.org/docs/2.4/howto/access.html ). 根据您的托管设置方式,您可以修改PHP的包含路径( http://php.net/manual/en/ini.core.php#ini.include-path )或将各种文档/目录限制为Apache站点配置中的特定主机/子网/无访问权限( https://httpd.apache.org/docs/2.4/howto/access.html )。

If you are on shared hosting, this level of lock down isn't usually possible, so you are stuck with using the Apache rewrite rules using a combination of a easy to handle file naming convention (ie, classFoo.inc.php and classBar.inc.php), the .htaccess file and using the FilesMatch directive to block access to *.inc.php - http://www.askapache.com/htaccess/using-filesmatch-and-files-in-htaccess/ 如果您使用的是共享主机,则通常无法实现此级别的锁定,因此您无法使用易于处理的文件命名约定(即classFoo.inc.php和classBar)的Apache重写规则。 inc.php),。htaccess文件并使用FilesMatch指令阻止访问* .inc.php - http://www.askapache.com/htaccess/using-filesmatch-and-files-in-htaccess/

FWIW all else being equal the Apache foundation says it is better/more efficient to do it in server side config vs. using .htaccess IF that option is available to you. FWIW所有其他方面都相同,Apache基金会表示在服务器端配置中使用.htaccess更好/更有效。如果您可以使用该选项。

A real-life application often uses a so-called public/ or webroot/ folder in the root of the project where all files to be requested over the web reside in. 现实生活中的应用程序通常在项目的根目录中使用所谓的public/webroot/文件夹,其中所有要通过Web请求的文件都驻留在该文件中。

This .htaccess file then forwards all HTTP requests to this folder with internal URL rewrites like the following: 然后,此.htaccess文件将所有HTTP请求转发到此文件夹,并使用内部URL重写,如下所示:

RewriteRule    ^$    webroot/  [L] # match either nothing (www.mydomain.com)
RewriteRule    ^(.*)$ webroot/$1 [L] # or anything else (www.mydomain.com/home.php)

.htaccess uses regular expressions to match the request URI (everything in the URL after the hostname) and prepends that with webroot/ , in this example. .htaccess使用正则表达式来匹配请求URI(主机名后面的URL中的所有内容),并在此示例中使用webroot/作为webroot/

www.mydomain.com/home.php becomes www.mydomain.com/webroot/home.php , www.mydomain.com/folder/file.php becomes www.mydomain.com/webroot/folder/file.php www.mydomain.com/home.php变得www.mydomain.com/webroot/home.phpwww.mydomain.com/folder/file.php成为www.mydomain.com/webroot/folder/file.php

Note: this will not be visible in the url in the browser. 注意:这在浏览器的URL中不可见。

When configured properly, all files that are placed outside of this folder can not be accessed by a regular HTTP request. 如果配置正确,常规HTTP请求将无法访问位于此文件夹之外的所有文件。 Your application however (your php scripts), can still access those private files, because PHP runs on your server, so it has filesystem access to those files. 然而,您的应用程序(您的PHP脚本)仍然可以访问这些私有文件,因为PHP在您的服务器上运行,因此它具有对这些文件的文件系统访问权限。

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

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