Search code examples
phpif-statementphpbb

Enable IF Statements in External PHPBB Pages


I'm using if statements in my PHPBB template, for example

<!-- IF S_USERNAME eq 'Stoker' -->Some content here<!-- ENDIF -->

This works great and really well. However, I have added some additional pages, and in order to ensure I can still call values such as their username, I've had to add this code to each header of the new pages.

define('IN_PHPBB', true);
define('ROOT_PATH', "../");

if (!defined('IN_PHPBB') || !defined('ROOT_PATH')) {
    exit();
}

$phpEx = "php";
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : ROOT_PATH . '/';
include($phpbb_root_path . 'common.' . $phpEx);

$user->session_begin();
$auth->acl($user->data);

This works great, and I can call and echo the username for example.

However, If statements are just not working, so if I did try

<!-- IF S_USERNAME eq 'Stoker' -->Some content here<!-- ENDIF -->

It just doesn't work, "some content here" is displayed, but I know the if statement isn't working. Am I missing some code in the header so that I can use if statements in PHPBB?


Solution

  • If you are using raw PHP, then you can use something like this:

    if ($user->data['username'] == 'Stoker') {
        // Do whatever needs doing
    }
    

    or, to make the end code look a little cleaner assign $user->data['username'] to a less cluttered looking variable, eg:

    $username = $user->data['username'];
    if ($username == 'Stoker') {
        // Do whatever needs doing
    }
    

    Alternatively, you can make your extra pages use the templating system to look exactly like they 'belong' to the same site as the forum.....

    extrapage.php would sit in the same directory as your root index.php and look like this: (if you want it in a subfolder, just change $phpbb_root_path to suit)

    <?php
    define('IN_PHPBB', true);
    $phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
    $phpEx = substr(strrchr(__FILE__, '.'), 1);
    include($phpbb_root_path . 'common.' . $phpEx);
    
    // Start session management
    $user->session_begin();
    $auth->acl($user->data);
    $user->setup();
    
    page_header('Title Here'); // this is used in the page title
    
    $template->set_filenames(array(
        'body' => 'extrapage.html',
    ));
    
    // Any PHP you need to use goes here, including assigning any additional template variables etc
    
    make_jumpbox(append_sid("{$phpbb_root_path}viewforum.$phpEx"));
    page_footer();
    ?>
    

    Then in /styles/your_style/template/ you would have your template file extrapage.html...

    <!-- INCLUDE overall_header.html -->
    
    <p>Your HTML goes here, you can use any global template variable in addition to variables declared in the above PHP file</p>
    
    <!-- INCLUDE overall_footer.html -->