Search code examples
phpincluderelative-pathphpbbdocument-root

Relative path issue when including phpBB code


I am currently making a website that is fused with a phpBB forum. Following the official solution to phpBB3 Sessions Integration I have used the following code to include relevant files from my phpBB install:

define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : 'forums/';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);
include($phpbb_root_path . 'includes/functions_display.' . $phpEx);

My forum folder is (root path)/forums/, so this works perfectly as long as the executing script is in the root folder of the domain. However, if I try to include this code from another folder, it throws the following error:

[phpBB Debug] PHP Warning: in file /home/unrealsp/public_html/includes/phpbb.php on line 5: include(forums/common.php) [function.include]: failed to open stream: No such file or directory [phpBB Debug] PHP Warning: in file /home/unrealsp/public_html/includes/phpbb.php on line 5: include(forums/common.php) [function.include]: failed to open stream: No such file or directory [phpBB Debug] PHP Warning: in file /home/unrealsp/public_html/includes/phpbb.php on line 5: include() [function.include]: Failed opening 'forums/common.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') [phpBB Debug] PHP Warning: in file /home/unrealsp/public_html/includes/phpbb.php on line 6: include(forums/includes/functions_display.php) [function.include]: failed to open stream: No such file or directory [phpBB Debug] PHP Warning: in file /home/unrealsp/public_html/includes/phpbb.php on line 6: include(forums/includes/functions_display.php) [function.include]: failed to open stream: No such file or directory [phpBB Debug] PHP Warning: in file /home/unrealsp/public_html/includes/phpbb.php on line 6: include() [function.include]: Failed opening 'forums/includes/functions_display.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php')

My solution attempt was as follows:

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

to

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

but that just broke it when run from the domain root, too, rather than fixing it, which is also something I don't understand.

How to include my phpBB code reliably from any location on the server?

Addendum: I am currently using a temporary URL for my webspace, which is made up of an URL with a user folder, like http://XXX.XXX.XXX.XXX/~user/. However, changing the variable to '/~user/forums/' didn't work, either.


Solution

  • I had the same problem with my site which I solved by getting the URL of the page and exploding it at every slash. I then used count() on the exploded array which tells me how deep into the directory structure the page is and used that number to add the required number of ../ 'up a directory' to the root path.