php 包含在 html 文件目录中

发布于 2024-07-08 16:49:30 字数 238 浏览 5 评论 0原文

所有 php 包含文件是否都必须位于包含目录中,或者这只是为了组织方便? 我可以使用该目录的路径包含任何目录中的文件吗? 包含 php 的 html 文件是否必须有 php 扩展名? 如果是这样,我想这意味着我的所有 html 文档都将是 php,如果它们都有我计划“包含”的菜单。 让网站上的每个文件都是 php 文件的协议是否可以接受?

鞠躬表示谦卑的敬意...

ps 有关于这个主题的好圣经吗!?

Do all php include files have to be in an include directory, or is that just an organizational convenience?
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?
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". Is that acceptable protocol to have every file on your site be a php file?

A bow in humble reverence...

p.s. is there a good bible on this topic!?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

那请放手 2024-07-15 16:49:31

如果将 PHP 放入 .html 文件并 include() 它,它应该可以工作。 但是,如果有人直接从服务器请求 .html 文件,他们可能会看到所有未处理的 PHP 代码,这是危险的。 因此,如果您这样做,那么要么将这些文件放在不可通过 Web 访问的目录中(例如文档根目录之上),要么更改您的 Web 服务器配置,以便将 .html 文件视为 PHP。

您应该能够 include() 任何文件,但如果 PHP 中有 open_basedir 限制(您可能会在大多数网络主机上找到),则访问某些目录可能会出现问题

If you put PHP in a .html file and include() it, it should work. 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. Therefore if you are doing this then either put these files in a non-web-accessible directory (e.g. above the document root) or change your web server configuration so that .html files are treated as 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)

苏辞 2024-07-15 16:49:31

您可以包含服务器上任何位置的文件(当然,取决于文件访问权限和安全限制)。

使用包含目录有助于使事情井井有条,但这不是必需的。

如果文件包含 PHP 代码,则应将其保存为 .php 文件 - 否则,如果直接调用该文件,代码将无法运行。 这会给您留下一个根据访问方式不同而表现不同的文件,这可能不是您想要的,并且可能会构成安全威胁。

为了解决这个问题,我看到有些人覆盖文件类型,以便服务器将所有 .html 文件视为 PHP 脚本 - 我不推荐这样做,因为它会减慢所有文件的处理速度,即使它们不包含任何 PHP。 遵循最小惊喜原则 - 让您的 HTML 文件为 .html,您的 PHP 文件为 .php - 这样每个人都会更高兴。

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. 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. Follow the Principle of Least Surprise - let your HTML files be .html and your PHP files be .php - and everyone will be happier.

天冷不及心凉 2024-07-15 16:49:31

James Musser:

我认为您指的是 Apache SSI 现在包含的内容
(参见http://httpd.apache.org/docs/ 2.2/howto/ssi.html#standard-footer

不知道答案,我尝试了一下,这是我的结果

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

(使用 WinXP、Apache 2.2.3、PHP 5.2.26 和 CentOS 5.2、Apache 进行测试) 2.2.3,PHP 5.1.16)

所以你的答案是肯定的 - 如果你想通过 Apache SSI #include 调用 PHP 脚本,该脚本必须具有 .php 扩展名(或其他扩展名,即 .php4 .php5 等) ,它在 Apache 中注册为 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)

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)

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.

っ左 2024-07-15 16:49:30

你不需要让你的 php 包含任何特定的地方,或者有任何文件扩展名...

调用 include('/path/to/your/file/with/any/extension.extension'); 应该将其包含为 php 文件

通常,使用 php 扩展名调用包含 php 代码的文件会更安全。

You don't need to have your php includes anywhere particular, or have any file extension...

Calling include('/path/to/your/file/with/any/extension.extension'); should include it as a php file

Normally safer to call files with php code in with a php extension tho.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文