简体   繁体   English

php包含在html文件目录中

[英]php includes in html files-directories

Do all php include files have to be in an include directory, or is that just an organizational convenience? 所有php包含文件都必须在include目录中,还是只是组织上的便利? Can I include files from any directory using the path to that directory? 我可以使用该目录的路径从任何目录中包含文件吗? Do html files that contain php includes have to have a php extension? 包含php的html文件是否必须有php扩展名? If so, I guess that would mean all of my html docs would be php if they all have the menus I am planning to "include". 如果是这样,我想这意味着如果他们都有我计划“包含”的菜单,我所有的html文档都将是php。 Is that acceptable protocol to have every file on your site be a php file? 是否可接受的协议使您网站上的每个文件都是一个php文件?

A bow in humble reverence... 谦卑敬畏的弓......

ps is there a good bible on this topic!? ps这个主题有一个很好的圣经!?

You don't need to have your php includes anywhere particular, or have any file extension... 你不需要将你的php包含在任何特定的地方,或者有任何文件扩展名......

Calling include('/path/to/your/file/with/any/extension.extension'); 调用include('/ path / to / your / file / with / any / extension.extension'); should include it as a php file 应该包含它作为一个PHP文件

Normally safer to call files with php code in with a php extension tho. 使用php扩展程序通过php代码调用文件通常更安全。

If you put PHP in a .html file and include() it, it should work. 如果你将PHP放在.html文件中并包含()它,它应该可以工作。 However if someone requests the .html file directly from the server they could be able to see all your PHP code un-processed which is dangerous. 但是,如果有人直接从服务器请求.html文件,他们可以看到所有未处理的PHP代码,这是危险的。 Therefore if you are doing this then either put these files in a non-web-accessible directory (eg above the document root) or change your web server configuration so that .html files are treated as PHP. 因此,如果您正在执行此操作,则将这些文件放在非Web可访问的目录中(例如,在文档根目录上方)或更改Web服务器配置,以便将.html文件视为PHP。

You should be able to include() any file but you may get problems accessing some directories if there is an open_basedir restriction in PHP (which you will probably find on most webhosts) 您应该能够包含()任何文件,但如果PHP中存在open_basedir限制,您可能会在访问某些目录时遇到问题(您可能会在大多数虚拟主机上找到)

You can include files from anywhere on the server (depending on file access privileges and security restrictions, of course). 您可以从服务器上的任何位置包含文件(当然,取决于文件访问权限和安全限制)。

Using an includes directory helps keep things well organized, but is not required. 使用包含目录有助于保持组织良好,但不是必需的。

If a file contains PHP code, it should be saved as a .php file - otherwise if it is called directly the code will not run. 如果文件包含PHP代码,则应将其保存为.php文件 - 否则如果直接调用该代码将无法运行。 This leaves you with a file which behaves differently depending on how it is accessed, which is probably not what you wanted and could be a security threat. 这将为您留下一个文件,该文件的行为方式会有所不同,具体取决于访问方式,这可能不是您想要的,可能是安全威胁。

To get around this, I have seen some people override file-types so the server treats all .html files as PHP scripts - I do not recommend this, as it will slow processing of all files even if they do not include any PHP. 为了解决这个问题,我看到有些人覆盖了文件类型,因此服务器将所有.html文件视为PHP脚本 - 我不建议这样做,因为即使它们不包含任何PHP,它也会减慢所有文件的处理速度。 Follow the Principle of Least Surprise - let your HTML files be .html and your PHP files be .php - and everyone will be happier. 遵循最小惊喜原则 - 让您的HTML文件为.html,PHP文件为.php - 每个人都会更开心。

James Musser: 詹姆斯·穆瑟:

I think you are referring to Apache SSI includes now (see http://httpd.apache.org/docs/2.2/howto/ssi.html#standard-footer ) 我想你现在指的是Apache SSI包括(参见http://httpd.apache.org/docs/2.2/howto/ssi.html#standard-footer

Not knowing the answer, I tried it, and here are my results 不知道答案,我试过了,这是我的结果

Extension  Call method        PHP runs
 .html      browser            no
 .html      Apache #include    no
 .html      PHP include()      yes
 .php       browser            yes
 .php       Apache #include    yes
 .php       PHP include()      yes

(Testing done with WinXP, Apache 2.2.3, PHP 5.2.26 and CentOS 5.2, Apache 2.2.3, PHP 5.1.16) (使用WinXP,Apache 2.2.3,PHP 5.2.26和CentOS 5.2,Apache 2.2.3,PHP 5.1.16进行测试)

So your answer is YES - if you wish to call a PHP script through an Apache SSI #include, the script must have a .php extension (or some other extension, ie .php4 .php5 etc, which is registered with Apache as being a PHP script), otherwise it will NOT run. 所以你的答案是肯定的 - 如果你想通过Apache SSI #include调用PHP脚本,脚本必须有.php扩展名(或者其他扩展名,即.php4 .php5等,它在Apache注册为PHP脚本),否则它不会运行。

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

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