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.
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.
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.
$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.
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.
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&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.
April 8th, 2007 at 6:59 am
Man thanks a lot and a lot for this tutorial...
You saved my life..
April 8th, 2007 at 9:16 am
Glad to be of assistance.
April 15th, 2007 at 12:59 pm
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!
April 15th, 2007 at 2:12 pm
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
April 15th, 2007 at 8:08 pm
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
April 15th, 2007 at 10:09 pm
@ Mike: I will add a download able file, thanks for the suggestion.
It looks to me as if you have the wrong path defined:
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.
April 15th, 2007 at 10:12 pm
@ 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.
April 15th, 2007 at 10:16 pm
@ 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 . '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.
April 15th, 2007 at 11:06 pm
@ 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
April 17th, 2007 at 12:19 pm
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
April 17th, 2007 at 1:48 pm
@ 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.
April 17th, 2007 at 4:04 pm
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.
April 17th, 2007 at 6:23 pm
Glad you got it working!
And I hope you do.
April 19th, 2007 at 11:45 pm
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?
April 20th, 2007 at 12:35 am
@ Graham: It seems to me that you have the wrong URL set for the action attribute of the form tag.
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:
make it into:
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.
April 29th, 2007 at 12:40 am
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 ' '
April 29th, 2007 at 12:58 pm
@ 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.