Integrating your existing site into phpBB3

March 11, 2010

One way of making an existing site dynamic is to integrate it with a phpBB forum. This is actually very easy, and allows you to quickly pull data about your users; control page access by groups and more.

Page type

The page you're integrating phpBB with needs to be a php page! (pretty obvious really seeing as phpBB is a PHP forum).

Connect to phpBB to get variables, etc.

You will need to include a file containing the phpBB connection information (essentially plugs the page into the rest of phpBB). This file should contain the following:

<?php
define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : 'change_this_to_phpbb_dir';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);
// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup();
?>

Save this file as phpbb.php, or put this code in some other file you will include in every page you want to integrate. All this code does is define where phpbb can be found, and include the common.php file from within phpbb. It also starts a user session, which we can use on our page.

Get our page to interact with phpBB

So now we go to the page we want to integrate with phpBB. In this case I'm going to use a blank file as an example but obviously you can use any file. This bit of code checks to see if the user is logged in or not and displays an appropriate message:

<?php include_once("phpbb.php"); ?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>phpBB conn test</title>
</head>
<body>
<?php
// PHPBB LOGIN AUTH
if ($user->data['user_id'] == ANONYMOUS) {
?>
Welcome, anomalous!
<?php
} else {
?>
Welcome back, <?php echo $user->data['username_clean']; ?> | You have <?php echo $user->data['user_unread_privmsg']; ?> new messages
<?php } ?>
</body>
</html>

This will display "Welcome, anomalous!" if you're not logged into phpBB or "Welcome back, username | You have 0 new messages" if you are logged in. Note the placement of the include_once ABOVE the tag - this is required if you don't want any errors.

This is a very simple example and doesn't tell our user how to log in or log out...which are pretty critical activities.

How about a login form?

The simple way to login is to push all logins through the phpBB system. This form does just that.

<form method="POST" action="forum/ucp.php?mode=login">
Username: <input type="text" name="username" size="20"><br />
Password: <input type="password" name="password" size="20"><br />
Remember Me?: <input type="checkbox" name="autologin"><br />
<input type="submit" value="Login" name="login">
<input type="hidden" name="redirect" value="">
</form>

This collect the login data and posts it to the phpBB ucp.php. We can make phpBB redirect the user back to any page by changing the value of the redirect input field.

<input type="hidden" name="redirect" value="../somefile.php">

Once this form has been posted the user will be logged in. But how about logging out?

Logging out of phpBB

You could just navigate to the forum and click the link there, but hey - might as well do this in as few clicks as possible. So we need a link to log out...

<a href="somefile.php?cp=logout">Log Out</a>

and in the same file we could put the following at the top of the document:

<?php
// check for logout request
$cp = $_GET['cp'];
// is it a logout? then kill the session!
if ($cp == "logout") {
$user->session_kill();
$user->session_begin();
echo "Successfully Logged Out.";
}
?>

So when cp is set to logout (when the user visits somefile.php?cp=logout) the session containing the userdata is destroyed and reset. 'Succssfully Logged Out' is also shown for the user's benefit.

Combining conditional, login & logout

As a summary, I've combined what we looked at above.

In phpbb.php:

<?php
define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH :  'change_this_to_phpbb_dir';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);
// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup();
?>

In somefile.php:

<?php include_once("phpbb.php");
// check for logout request
$cp = $_GET['cp'];
// is it a logout? then kill the session!
if ($cp == "logout") {
$user->session_kill();
$user->session_begin();
echo "Successfully Logged Out.";
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>phpBB conn test</title>
</head>
<body>
<?php
// Page login notice
if ($user->data['user_id'] == ANONYMOUS)
{
?>
<center>Welcome! Please login below or click <a href="forum/ucp.php?mode=register">here</a> to register.<br />
<form method="POST" action="forum/ucp.php?mode=login">
Username: <input type="text" name="username" size="20"><br />
Password: <input type="password" name="password" size="20"><br />
Remember Me?: <input type="checkbox" name="autologin"><br />
<input type="submit" value="Login" name="login">
<input type="hidden" name="redirect" value="../somefile.php">
</form>
<?php
} else { ?>
Welcome back, <?php echo $user->data['username_clean']; ?> | You have <?php echo $user->data['user_unread_privmsg']; ?> new messages | <a href="somefile.php?cp=logout">Log Out</a>
<?php } ?>
</body>
</html>

This will give you basic integration with an existing phpBB forum, we'll look at further integration including private messenging in contact boxes soon.


Profile picture

From Dave, who writes to learn things. Thoughts and views are his own.

© 2024, withdave.