使用绝对路径以便将来更容易修改包含路径?

发布于 2024-08-29 06:17:59 字数 502 浏览 8 评论 0原文

config.php 放在根级别,该文件将包含在任何页面中。

然后在 config.php

<?php
define( 'ROOT_DIR', dirname(__FILE__) );
?>

因此,在来自不同 sub/a.php 、 sub/sub/b.php 目录的所有其他页面,当我想在特定位置包含特定文件时,我只需要

include( ROOT_DIR.'/include/functions.php' );

在 Windows 服务器中,将 ROOT_DIR 带C:/inetpub/vhosts/domain.com 的值

这是一个好的/安全的方法吗?

看来通过这种方式,当我将b.php移动到其他上层文件夹时,我不需要对包含文件路径进行任何更改,这有利于维护。

有什么缺点吗?比如 SEO 明智的,或者任何其他原因...... 你们怎么想。

config.php put at the root level, this file will be included in any pages.

Then at config.php

<?php
define( 'ROOT_DIR', dirname(__FILE__) );
?>

So at all other pages from different sub/a.php , sub/sub/b.php directories, when I want to include a specific file in specific location, I just need to

include( ROOT_DIR.'/include/functions.php' );

In windows server, the ROOT_DIR bring the value to C:/inetpub/vhosts/domain.com

Is this a good/secure way?

It seems like via this way, when I move the b.php to other upper level folder, I don't need to do any changes to the include file path, which is good for maintenance.

Any cons? Like SEO wise, or any other reason...
What you guys think.

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

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

发布评论

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

评论(3

一百个冬季 2024-09-05 06:17:59

我要评论的是,您使用的方法(使用绝对路径)的一个优点是 PHP 不需要解析每个请求的路径。这样您可能会看到性能稍微好一些。

另外,如果您使用的是 PHP 5.3,则可以仅使用 __DIR__ 而不是 dirname(__FILE__)

如果您不使用 5.3,您可以尝试一下。 5.3 中针对 Windows 平台做了很多改进,更不用说许多新的有用的语言功能了。

I will comment that one advantage to the method you're using (using absolute paths) is that PHP won't need to resolve the path for every request. You may see ever-so-slightly better performance that way.

Also, if you're using PHP 5.3, you can just use __DIR__ instead of dirname(__FILE__).

If you're not using 5.3, you might give it a shot if you can. A lot of improvements were made for the Windows platform in 5.3, not to mention the many new useful language features.

若水般的淡然安静女子 2024-09-05 06:17:59

我认为这样做是非常明智的,因为它显着增加了站点的可移植性。我不明白这会对 SEO 有什么影响。

但是,此方法并不允许您永远更改包含路径。您仍然需要更改 config.php 的包含路径,因为在包含该路径之前,ROOT_DIR 显然尚未定义。

如果您想避免这样做,您应该使用 $_SERVER['DOCUMENT_ROOT'] 代替 ROOT_DIR。这将返回相同的内容,并且由于它可用于所有脚本,因此可用于包含 config.php 以及所有其他包含内容。

或者,您可以 编辑 php.ini 中的包含路径。但是,这假设 a) 所有包含内容都位于同一文件夹中,并且 b) 您有权访问服务器上的 php.ini。

I think doing something like this is highly advisable since it adds significantly to the portability of your site. I don't see how this could have any effect on SEO.

However, it is not true that this method allows you to never change your include paths. You will still have to change the include path for config.php as ROOT_DIR is not obviously not defined until after this has been included.

If you want to avoid doing this, you should use $_SERVER['DOCUMENT_ROOT'] in place of ROOT_DIR. This will return the same thing and as it is available to all of your scripts, can be used to include config.php as well as all your other includes.

Alternatively, you can edit the include path in php.ini. However, this assumes that a) all of your includes are in the same folder and b) that you have access to php.ini on your server.

知足的幸福 2024-09-05 06:17:59

我建议使用虚拟主机...

C:/inetpub/vhost/account/html = 指向=>;域名.com
C:/inetpub/vhost/account/includeded_files

define('PATH_INCLUDE','C:/inetpub/vhost/account/included_files');

I suggest using vhosts as such...

C:/inetpub/vhost/account/html = points to => domain.com
C:/inetpub/vhost/account/includeded_files

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