Knowledge Base

phpBB2 Sessions Integration
Article ID: 33
Written By: ckwalsh
Written On: Wed May 02, 2007 3:46 am
Description: How to access phpBB session information from your own pages, allowing you to check user's logged in status, user name, user id, and more.
link to this article on phpbb.com: Select All
[kb=phpbb2-sessions-integration]phpBB2 Sessions Integration[/kb]
link to this article on your own board: Select All
[url=http://www.phpbb.com/kb/article/phpbb2-sessions-integration/]Knowledge Base - phpBB2 Sessions Integration[/url]

First Things First

Before you integrate any pages, you must first adjust the cookie path settings for your site. Unless you have changed it, phpBB cookies are only sent when one of your visitors is viewing your forum, not when they are looking at the rest of your site. Go to your admin control panel and change the cookie path (not script path!) setting to "/". After that, log out of your forum and delete your browser's cache and the cookies for your site. You are now ready to integrate the rest of your site!

Required Code

On every page you wish to integrate the phpBB sessions into, the page must first begin with the following code:
Code: Select all
<?php
define('IN_PHPBB', true);
$phpbb_root_path = './forum/';
include($phpbb_root_path . 'extension.inc');
include($phpbb_root_path . 'common.'.$phpEx);

//
// Start session management
//
$userdata = session_pagestart($user_ip, PAGE_INDEX);
init_userprefs($userdata);
//
// End session management
//
?>


However, this code must be modified to work on your site. In the second line, the $phpbb_root_path variable must be changed to point to where your forum is located. For example, if your forum is located in a folder named "phpBB", the path would be "./phpBB/". The "./" at the beginning of the path is very important, as it helps secure the page from hackers.

What can I do now?
Now it is possible for you to use all the phpBB system variables, such as the $userdata variable, in your own PHP scripts. Here is a basic example:
Code: Select all
<?php
define('IN_PHPBB', true);
$phpbb_root_path = './forum/';
include($phpbb_root_path . 'extension.inc');
include($phpbb_root_path . 'common.'.$phpEx);

//
// Start session management
//
$userdata = session_pagestart($user_ip, PAGE_INDEX);
init_userprefs($userdata);
//
// End session management
//

if($userdata['session_logged_in'])
{
echo('Hi ' . $userdata['username'] . '!');
}
else
{
echo('You are a guest');
}

?>


You also have access to the $db and $template variables, as well as a couple more. Experiment and feel free to post questions in the MOD authors forum.

How can I login from a page I made?
Logging in from another page on your site is very easy. Here is a template for a login form:
Code: Select all
<form action="forum/login.php" method="post" enctype="multipart/form-data">
<input type="text" name="username"><br />
<input type="password" name="password"><br />
<input type="hidden" name="redirect" value="../">
<input type="submit" value="login" name="login">
</form>


Again, The code must be modified to work. The form action must reflect where the forum is actually located so it can reach login.php. In this case, it is in a folder called "forum". The redirect field is optional, but if present with change where your users are directed after logging in. This is relative to where your forum is, so setting it to "mypage.php" will direct users to "www.example.com/forum/mypage.php". In the example above it is set to "../", which will direct users to the parent directory, http://www.example.com if the forum is at http://www.example.com/forum.

How Do I make a logout Link?
The logout URL has the general format of "login.php?logout=true&amp;sid=0123456789abcdef0123456789abcdef". Notice how it includes the session ID of the user. While it is possible to add this to the link using $userdata['session_id'] variable, it can be added automatically using the append_sid() function. I won't go into the details of using this function here, since there is a very good KB article explaining it Here.

Below is an example script that displays a valid logout link for logged in users.
Code: Select all
<?php
define('IN_PHPBB', true);
$phpbb_root_path = './forum/';
include($phpbb_root_path . 'extension.inc');
include($phpbb_root_path . 'common.'.$phpEx);

//
// Start session management
//
$userdata = session_pagestart($user_ip, PAGE_INDEX);
init_userprefs($userdata);
//
// End session management
//

if($userdata['session_logged_in'])
{
echo('<a href="' . append_sid($phpbb_root_path . 'login.php?logout=true') . '">Logout</a>');
}
else
{
echo('You are a guest');
}

?>

You can also change where users are redirected when after they logout by adding "&redirect=newpage.php" to the url. Like the login redirect, it also is relative to the forum root.

Below is example code you can use as a template for your own pages. Feel free to modify it and use it as you wish.

Code: Select all
<?php
define('IN_PHPBB', true);
$phpbb_root_path = './forum/';
include($phpbb_root_path . 'extension.inc');
include($phpbb_root_path . 'common.'.$phpEx);

//
// Start session management
//
$userdata = session_pagestart($user_ip, PAGE_INDEX);
init_userprefs($userdata);
//
// End session management
//
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Example sessions page</title>
<body>
<?php
if($userdata['session_logged_in'])
{
echo('Hi '.$userdata['username'].'! <a href="' . append_sid($phpbb_root_path . 'login.php?logout=true&redirect=..%2F') . '">Logout</a>');
}
else
{
?>

Hi Guest!<br />
<form action="<?php echo($phpbb_root_path); ?>login.php" method="post" enctype="multipart/form-data">
Username: <input type="text" name="username"><br />
Password: <input type="password" name="password"><br />
<input type="hidden" name="redirect" value="../">
<input type="submit" value="login" name="login">
</form>
<?php
}

?>
</body>
</html>