Question

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's approach is to include the following code into each php document:

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.

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.

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).

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.

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).

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/".

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?

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top