绝对/相对路径:PHP和HTML之间的根差异

时间:2022-05-10 13:25:58

I am currently trying to integrate my phpBB installation into a custom CMS I'm building. phpBB relies on a variable named $phpbb_root_path in many of its functions, which works both behind the scenes (for inclusion of other PHPs etc) as well as creating front-end hyperlinks and image links.

我目前正在尝试将我的phpBB安装集成到我正在构建的自定义CMS中。 phpBB在其许多函数中依赖于一个名为$ phpbb_root_path的变量,它在幕后工作(包含其他PHP等)以及创建前端超链接和图像链接。

phpBB's approach is to include the following code into each php document:

phpBB的方法是在每个php文档中包含以下代码:

define('IN_PHPBB', true);

$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : 'forums/';
print_r(dirname(__FILE__));

$phpEx = substr(strrchr(__FILE__, '.'), 1);
require_once( $phpbb_root_path . 'common.' . $phpEx);
require_once($phpbb_root_path . 'includes/functions_display.' . $phpEx);

The $phpbb_root_path variable is then changed according to the document's location in relation to the forums. This is a functioning approach, however I wish to include the code from a separate .php file via require_once instead, to avoid issues with my custom template system, and so that I don't have to copypaste the same code into every document I have.

然后根据文档相对于论坛的位置更改$ phpbb_root_path变量。这是一种功能正常的方法,但我希望通过require_once包含来自单独的.php文件的代码,以避免我的自定义模板系统出现问题,这样我就不必将相同的代码复制到我拥有的每个文档中。

The symptoms: The above code, if included, will work fine as long as the including document is in my document root (of which /forums/ is a subdirectory), but it will invariably break anywhere else, or if used in connection with .htaccess url_rewrite.

症状:上面的代码(如果包含的话)只要包含文档在我的文档根目录(其中/ forums /是子目录)中就可以正常工作,但它总是在其他地方中断,或者如果与之结合使用。 htaccess url_rewrite。

The issue: It's not possible to simply define $phpbb_root_path as '/forums/' or '/~(USERID)/forums/', which would be the obvious, easy solution. Why? Because there is a discrepancy between what HTML and PHP regard as root (the leading slash).

问题:不可能简单地将$ phpbb_root_path定义为'/ forums /'或'/〜(USERID)/ forums /',这将是一个明显,简单的解决方案。为什么?因为HTML和PHP视为root(前导斜杠)之间存在差异。

By printing dirname(__FILE__) I determined that my php installation regards /home/(USERID)/public_html/ as root. HTML, however, (obviously) takes WWW.DOMAIN.COM as root.

通过打印dirname(__ FILE__)我确定我的php安装将/ home /(USERID)/ public_html /视为root。但是,HTML(显然)将WWW.DOMAIN.COM作为root用户。

As you can imagine, this leads to problems:

你可以想象,这会导致问题:

1) If I define $phpbb_root_path as /home/(USERID)/public_html/, the PHP part (includes etc) will work, but any functions that print HTML will result in screwups, like <img>s linking to (WWW.DOMAIN.COM)/home/(USERID)/public_html/(link to image).

1)如果我将$ phpbb_root_path定义为/ home /(USERID)/ public_html /,PHP部分(包括etc)将起作用,但任何打印HTML的函数都会导致搞砸,例如绝对/相对路径:PHP和HTML之间的根差异 s链接到(WWW.DOMAIN) .COM)/ home /(USERID)/ public_html /(链接到图像)。

2) If I define $phpbb_root_path as /forums/, the page will not display at all, as the PHP will try to find a directory called "/forums/" on the same level as "/home/".

2)如果我将$ phpbb_root_path定义为/ forums /,则页面将根本不显示,因为PHP将尝试在与“/ home /”相同的级别上找到名为“/ forums /”的目录。

Question: Is there an easy/clever way to remove this discrepancy between PHP/HTML root folder definitions, for example by attuning the PHP root to be /home/(USERID)/public_html/? Or do I not have any choice but include the code in every document and use relative paths according to where the document is located?

问题:是否有一种简单/巧妙的方法可以消除PHP / HTML根文件夹定义之间的差异,例如将PHP根目录调整为/ home /(USERID)/ public_html /?或者我没有任何选择,只是在每个文档中包含代码,并根据文档的位置使用相对路径?

1 个解决方案

#1


0  

get the page URL, explode it at the slashes, count the resulting array then use that number to determine how deep into the directory structure you are to decide the relative URL for the root path

获取页面URL,在斜杠上展开它,计算生成的数组,然后使用该数字确定目录结构的深度,以决定根路径的相对URL

#1


0  

get the page URL, explode it at the slashes, count the resulting array then use that number to determine how deep into the directory structure you are to decide the relative URL for the root path

获取页面URL,在斜杠上展开它,计算生成的数组,然后使用该数字确定目录结构的深度,以决定根路径的相对URL