开发者

Use absolute path for easier modify include path in future?

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 loca开发者_开发知识库tion, 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.


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.


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.


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');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜