PHP: PHPBB2 integration with your site

In this tutorial I will teach you how to integrate PHPBB2 into your main site. This includes a login form, showing user info, and more.

PHPBB2 keeps sessions using cookies. Making the cookies extend to your main site is a fairly simple task.

There is a small complication with keeping the PHPBB2 sessions active when linking from your integrated main site to your forum. You have to use the append_sid() function on all the links to your forum. It is also recommended to do this with all links that point to the mainsite, but it is not required.

Lets start with a simple file that we will include in all our main site pages. This file will take care of the cookies PHPPBB2 uses and allow us to use PHPBB2 defined functions.

<?php
define('IN_PHPBB', true);
$phpbb_root_path = 'forums/'; //Relative path to your PHPBB2 installation
include($phpbb_root_path . 'extension.inc');
include($phpbb_root_path . 'common.'.$phpEx);
$userdata = session_pagestart($user_ip, PAGE_INDEX);
init_userprefs($userdata);
?>

Make sure that the variable with the path to your forum is correct, otherwise the sessions will not work.

Now lets make an example page witch includes the above file and checks if a user is logged in and if the user is a guest displays a login form.

<?php
include('include.php'); //include our session handling file, make sure you have the right file name

if( !$userdata['session_logged_in'] ) // Is the user NOT logged in?
   {
      ?>
<form action="forums/login.php" method="post" name="login"> <!-- Show a simple login form -->
<input type="text" name="username"><br />
<input type="password" name="password"><br />
<input type="hidden" name="redirect" value="../index.php"> <!-- Redirect the user to a page different than the PHPBB2 index page. You can delete this if you want. The path must be relative from the PHPBB2 login file. -->
<input type="submit" value="login" name="login">
</form>
      <?php
   }
?>

Make sure that the action attribute of the form tag points to your forums login page.

Now for the append_sid() function, here is an example of how the function can be use on your URLs.

<?php
$phpbb_links = array ( //start our array
'home' => append_sid("index.$phpEx"), // .$phpEx is the way PHPBB2 writes the .php file extension, change this to just .html if you use .html files or other file extensions.
'forums_home' => append_sid("forums/index.$phpEx"),
'tolist' => append_sid("toplist.$phpEx"), // notice the comma after each line
'downloads' => append_sid("downloads.$phpEx") // there is no comma on the last line
); // end our array
?>

You can simply put this code at the top of your page or even better put it in the sessions file that you include on each page.

Then you simply call the variable when you want the link.

<a href="<?php echo $phpbb_links['home']; ?>">Site Home</a>

It's as simple as that. The last thing I will show you how to do is to display some information about a user when he or she is logged in.

<?php
include('include.php'); //include our session handling file
if( $userdata['session_logged_in'] ) // Is the user logged in?
   {
    $appendLogout = $u_login_logout = $phpbb_root_path.'login.'.$phpEx.'?logout=true&amp;sid=' . $userdata['session_id']; // Add the session ID to the logout link
    echo "Welcome back, <a href=\"forums/profile.php?mode=viewprofile&u=".$userdata['user_id']."\" title=\"".$userdata['username']."\">".$userdata['username']."</a>!<br />"; // Show a welcome message
    echo "<a href=\"forums/privmsg.php?folder=inbox\" title=\"You have ".$userdata['user_unread_privmsg']." new messages\">(".$userdata['user_unread_privmsg'].") New Messages</a><br />"; // Any new PMs?
    echo "<a href=\"forums/profile.php?mode=editprofile\" title=\"My Profile\">My Profile</a><br />"; // Edit your profile link
    echo "<a href=\"".$appendLogout."\" title=\"Logout\">Logout</a><br />"; // Logout link
   } // end if, if you want you could add a login form in an else statement below
?>

Make sure you make the links point to your forums, if you want you can change all the links to use the $phpbb_root_path set in the included file as I have done in the logout link. Also notice that i didn't use append_sid() on the logout link, it is required to add the session id manually on a logout link otherwise you will get an error from the logout page.

You can download the entire script in .zip format from:
http://www.marek.litomisky.com/tutorials/PHPBB2-integration/PHPBB2-integration.zip
Just make sure you change the forum path to the correct one.

That's about all there is to site integration with PHPBB2! If you have any questions or comments leave them using the form below and if you liked this tutorial, please think about registering at Design Avarice.

17 Responses to “PHP: PHPBB2 integration with your site”

  1. Uğur Aydoğdu Says:

    Man thanks a lot and a lot for this tutorial...
    You saved my life..

  2. Hepa7 Says:

    Glad to be of assistance.

  3. Mike Says:

    Awesome man, I was waiting for some of these. The other tutorials I read were all crappy, I am gonna have to say that this was the best tutorial I have read, out of all things! Even Photoshop tutorials!

    One thing- if you could include a download file with this, then you would have made my day!

    Awesome!

  4. Mike Says:

    I get errors when I try this-

    Warning: main(/forums/extensions.inc): failed to open stream: No such file or directory in /.../nrxdesigns.com/include.php on line 7

    Warning: main(): Failed opening '/forums/extensions.inc' for inclusion (include_path='/.../nrxdesigns.com/include.php on line 7

    Warning: main(/forums/common.php): failed to open stream: No such file or directory in /.../nrxdesigns.com/include.php on line 9

    Warning: main(): Failed opening '/forums/common.php' for inclusion (include_path='/.../') in /.../nrxdesigns.com/include.php on line 9

    Fatal error: Call to undefined function: session_pagestart() in /.../nrxdesigns.com/include.php on line 11

  5. Anthony Says:

    I am trying to do it but I keep getting this error message

    Fatal error: Call to undefined function: session_pagestart() in /homepages/36/d170112165/htdocs/Bstone/forums.php on line 44

    this is the way I have it

  6. Hepa7 Says:

    @ Mike: I will add a download able file, thanks for the suggestion.

    It looks to me as if you have the wrong path defined:

    $phpbb_root_path = 'forums/'; //Relative path to your PHPBB2 installation

    You can't have a path like http://www.nrxdesigns.com/forums it has to be relative from the file from witch you are including the file so if your structure is like this:

    /index.php
    /include.php
    /forums/
    /forums/common.php
    /forums/extensions.inc
    etc.

    the correct file path to put in include.php is: forums/

    If that is not the problem then you might be using a different version of PHPBB, PHPBB3 as been released a while ago and this is designed for PHPBB2.

    I have a test PHPBB2 installation at:
    http://www.marek.litomisky.com/projects/phpbb2/

    Here is a file using the exact code (with the forum path changed) above and it works fine:
    http://www.marek.litomisky.com/projects/phpbb2/test.php

    Maybe if you can post your directory structure I can help you more.

  7. Hepa7 Says:

    @ Mike: I have looked at your forums, it appears to be the right version, so the problem is most likely with the path you defined.

  8. Hepa7 Says:

    @ Anthony: It looks like you aren't including the files that the function is defined in. The files are included in the include.php file:

    include($phpbb_root_path . 'extension.inc');
    include($phpbb_root_path . 'common.'.$phpEx);

    If you do not include those files you cannot use the function. If you have the code, and it is correct you might be using a different version of PHPBB than the one I made this script for.

  9. Hepa7 Says:

    @ Anthony: I have looked over your site and it seems to me you are trying to get the actual message board on your site?

    If that is what you are trying to do the correct approach would be to skin the forums to look like your main site not attempt to somehow get it on your site.

    If you can skin the forums right they will fit into your site perfectly.

    If that is what you want to do here is a simple skinning tutorial:
    http://www.phpbb.com/community/viewtopic.php?t=10764

  10. Anthony Says:

    What I am trying to do is put the message board inside my theme so I can also use my menu on my site to get back to other places in my site = integrate

  11. Hepa7 Says:

    @ Anthony: Yes, thats exactly what I meant, the code in this tutorial is to extend the PHPBB2 user login to your general site, not to actually show the forums on the site.

    To do what you want you have to make a template for your forums that has the links you want to your main site and looks like your main site.

    Making a custom skin for your forums isn't very hard it just takes a lot of work. Again you can find the basics of how to skin PHPBB2 at:
    http://www.phpbb.com/community/viewtopic.php?t=10764

    If you don't want to make it yourself there are companies on the web that make customs skins for PHPBB2 just Google it.

  12. Mike Says:

    Very nice, now, thanks! I just deleted the files I made, and I replaced it with your superior files. It worked totally.

    I will think about registering with you now!

    I am very grateful for you.

  13. Hepa7 Says:

    Glad you got it working!

    And I hope you do.

  14. Graham Says:

    Hi

    Love the script, just one problem though. When I set up my form on the page and I input my login details and click the login button I get an error 404 page cannot be found. However when I manual enter the URL of the login page it comes up fine?

    Any ideas?

  15. Hepa7 Says:

    @ Graham: It seems to me that you have the wrong URL set for the action attribute of the form tag.

    <form action="forums/login.php" method="post" name="login"> <!-- Show a simple login form -->

    You have to make sure that the URL points to your login file in the PHPBB folder. When you manually go to your login page copy the URL from the address bar and paste it in the action attribute in the form tag.

    You should also make sure the URL doesn't have any variables attached. So if the URL is like this:

    http://www.planet-loud.com/forums/login.php?ID=skldaskg&type=register

    make it into:

    http://www.planet-loud.com/forums/login.php

    Try that and if it doesn't work it would help me to help you if you could post your directory structure so i can see where your files are located.

  16. mol Says:

    Hi, I've tried your codes and had this error:

    "Call a member function on a non-object in forums/includes/sessions.php"

    What is it ? '______' I'm sure that I did everything you said ' '

  17. Hepa7 Says:

    @ mol: I"m not quite sure what the problem you are having is.

    Could you please post the exact code you are using, and your directory structure, showing where the include file is, where the forums are, etc.?

    I should be able to solve the problem with that information. Also if you can post a link to the files on your server so i can see thew output myself.

Leave a Reply

Sorry, comment posting is disabled untill the new CMS is done.