Dynamic Sites With PHPBB

In this tutorial, I will show you how to make your whole site more dynamic, by including the PHPbb login to work all all the pages of your site. This tutorial will hopefully help increase your knowledge of PHP, and mySQL. With some very simple, and easy modifications, which I include here, you can easily add Birthday Announcements, Users Online, Private Messages, and Recent Discussion to any page of your site. You can also use the PHPbb login information with other custom DB forms on your site. Expanding your site into an extremely dynamic community.

This tutorial will use many hacks from http://www.phpbbhacks.com/, some were greatly changed by me, and others. I will try my best to give credit to those who wrote the code, or came up with the idea.

1. Userdata Accessing.

In order to use the PHPbb login on all the pages of your site, you will need to add the correct code to the headers of all of your non-phpbb pages. I hope you used something easy like made a 'top.php' header you included in all the pages, or else you've got a lot of code to insert on every page. I will explain all the code, then have all the code you need later, so don't worry about copying it now.

First part to add, define in phpbb. This was a security measure added in version 2.0.0 of the software. Added to stop people from hacking into your forums, and in this case, even yourself.

define('IN_PHPBB', true);

Now, you will need to include the files PHPbb uses. This should be fairly simple, and easy to do, all you need to change are the absolute paths to your files.

$site_root_path = '/web/etc/you'; //<-- Modify
$phpbb_root_path2 = '/phpbb2/'; //<-- Modify
$phpbb_root_path = $site_root_path . $phpbb_root_path2;
include($phpbb_root_path . 'extension.inc');
include($phpbb_root_path . 'common.php');
include($phpbb_root_path . 'config.php');

Next, you will need to add the user management code, this actually does all the important work. It grabs the userdata from the database for the person if they are logged in. It also will define all the PHPbb variables you will end up using for the parts of your site.

$userdata = session_pagestart($user_ip, PAGE_INDEX);
init_userprefs($userdata);

Well that should be all you need to really actually make users accessible to your site. The very simple code can actually do pretty much all you would want to do, but in the next sections, I will show you how to go the extra mile. Here is the full code for this section:

define('IN_PHPBB', true);

$site_root_path = '/web/etc/you/'; //<-- Modify
$phpbb_root_path2 = '/phpbb2/'; //<-- Modify
$phpbb_root_path = $site_root_path . $phpbb_root_path2;
include($phpbb_root_path . 'extension.inc');
include($phpbb_root_path . 'common.php');

$userdata = session_pagestart($user_ip, PAGE_INDEX);
init_userprefs($userdata);

2. Who Is Online?

One of the things most people will want to add to their site, is a list showing who is online. This one will be very complex to modify to your liking, so it is recommended that if you want to change it, you be very skilled in PHP, this hack is copied straight from the PHPbb coding, and looks like it does on your forum. It is available in hack form by Acid at the link here: http://www.phpbbhacks.com/viewhack.php?id=201 Since it is so long, I won't be explaining what it does.

$user_forum_sql = ( !empty($forum_id) ) ? "AND ( u.user_session_page = $forum_id
OR s.session_page = $forum_id)" : "";

$sql = "SELECT u.username, u.user_id, u.user_allow_viewonline, u.user_level, s.session_logged_in, s.session_ip
FROM ".USERS_TABLE." u, ".SESSIONS_TABLE." s
WHERE u.user_id = s.session_user_id
AND ( s.session_time >= ".( time() - 300 ) . "
OR u.user_session_time >= " . ( time() - 300 ) . " )
$user_forum_sql
ORDER BY u.username ASC";
$result = $db->sql_query($sql);
if(!$result)
{
message_die(GENERAL_ERROR, "Couldn't obtain user/online information.", "", __LINE__, __FILE__, $sql);
}

$userlist_ary = array();
$userlist_visible = array();

$logged_visible_online = 0;
$logged_hidden_online = 0;
$guests_online = 0;
$online_userlist = "";

$prev_user_id = 0;
$prev_session_ip = 0;

while( $row = $db->sql_fetchrow($result) )
{
// User is logged in and therefor not a guest
if( $row['session_logged_in'] )
{
// Skip multiple sessions for one user
if( $row['user_id'] != $prev_user_id )
{
$style_color = "";
if( $row['user_level'] == ADMIN )
{
$row['username'] = '<b>' . $row['username'] . '</b>';
$style_color = 'style="color:#' . $theme['fontcolor3'] . '"';
}
else if( $row['user_level'] == MOD )
{
$row['username'] = '<b>' . $row['username'] . '</b>';
$style_color = 'style="color:#' . $theme['fontcolor2'] . '"';
}

if( $row['user_allow_viewonline'] )
{
$user_online_link = '<a href="' . append_sid($phpbb_root_path2."profile.$phpEx?mode=viewprofile&amp;" . POST_USERS_URL . "=" . $row['user_id']) . '"' . $style_color .'>' . $row['username'] . '</a>';
$logged_visible_online++;
}
else
{
$user_online_link = '<a href="' . append_sid($phpbb_root_path."profile.$phpEx?mode=viewprofile&amp;" . POST_USERS_URL . "=" . $row['user_id']) . '"' . $style_color .'><i>' . $row['username'] . '</i></a>';
$logged_hidden_online++;
}

if( $row['user_allow_viewonline'] || $userdata['user_level'] == ADMIN )
{
$online_userlist .= ( $online_userlist != "" ) ? ", " . $user_online_link : $user_online_link;
}
}
}
else
{
if( $row['session_ip'] != $prev_session_ip ){
$guests_online++;
}
}

$prev_user_id = $row['user_id'];
$prev_session_ip = $row['session_ip'];
}

if( empty($online_userlist) )
{
$online_userlist = $lang['None'];
}
$online_userlist = ( ( isset($forum_id) ) ? $lang['Browsing_forum'] : $lang['Registered_users'] ) . " " . $online_userlist;

$total_online_users = $logged_visible_online + $logged_hidden_online + $guests_online;

if($total_online_users > $board_config['record_online_users'])
{
$sql = "UPDATE " . CONFIG_TABLE . "
SET config_value = '$total_online_users'
WHERE config_name = 'record_online_users'";
if( !$result = $db->sql_query($sql) )
{
message_die(GENERAL_ERROR, "Couldn't update online user record (nr of users)", "", __LINE__, __FILE__, $sql);
}

$sql = "UPDATE " . CONFIG_TABLE . "
SET config_value = '" . time() . "'
WHERE config_name = 'record_online_date'";
if( !$result = $db->sql_query($sql) )
{
message_die(GENERAL_ERROR, "Couldn't update online user record (date)", "", __LINE__, __FILE__, $sql);
}

$board_config['record_online_users'] = $total_online_users;
$board_config['record_online_date'] = time();
}

if( $total_online_users == 0 )
{
$l_t_user_s = $lang['Online_users_zero_total'];
}
else if( $total_online_users == 1 )
{
$l_t_user_s = $lang['Online_user_total'];
}
else
{
$l_t_user_s = $lang['Online_users_total'];
}

if( $logged_visible_online == 0 )
{
$l_r_user_s = $lang['Reg_users_zero_total'];
}
else if( $logged_visible_online == 1 )
{
$l_r_user_s = $lang['Reg_user_total'];
}
else
{
$l_r_user_s = $lang['Reg_users_total'];
}

if( $logged_hidden_online == 0 )
{
$l_h_user_s = $lang['Hidden_users_zero_total'];
}
else if( $logged_hidden_online == 1 )
{
$l_h_user_s = $lang['Hidden_user_total'];
}
else
{
$l_h_user_s = $lang['Hidden_users_total'];
}

if( $guests_online == 0 )
{
$l_g_user_s = $lang['Guest_users_zero_total'];
}
else if( $guests_online == 1 )
{
$l_g_user_s = $lang['Guest_user_total'];
}
else
{
$l_g_user_s = $lang['Guest_users_total'];
}

$l_online_users = sprintf($l_t_user_s, $total_online_users);
$l_online_users .= sprintf($l_r_user_s, $logged_visible_online);
$l_online_users .= sprintf($l_h_user_s, $logged_hidden_online);
$l_online_users .= sprintf($l_g_user_s, $guests_online);

$onlinet="$l_online_users<br>$online_userlist<br>";

Now, in order to use what it just generated, echo the variable $onlinet in the page, and it will generate all the data. If you wish to change the way the data is displayed, you will need to modify the code above. Start at the top, as a lot of parts are used towards the bottom, where you want to customize.

3. Extending Private Messaging Capabilities

This was a hack, I had not seen done on any sites I've visited. So I decided to add it to my site, and create the hack in return. This will actually check and see if you have any new private messages, and pop-up a box if the user has it set to do so. I have written this as a hack available here: http://www.phpbbhacks.com/viewhack.php?id=710 The first part of the code is required, and will be used lots of places from now on, it checks if the user is logged in.

if($userdata['session_logged_in']){ //logged in

Don't forget to close that later on after the hack is finished, I'll remind you. This next part does all the money, it starts by looking and seeing if you have any new messages, if not it won't do anything. But if you do it will start checking and seeing if you do. Then it will update the table so it doesn't have to tell you twice.

if ( $userdata['user_new_privmsg'] ) //private messages by AJ Quick
{
$l_message_new = ( $userdata['user_new_privmsg'] == 1 ) ? $lang['New_pm'] : $lang['New_pms'];
$l_privmsgs_text = sprintf($l_message_new, $userdata['user_new_privmsg']);

if ( $userdata['user_last_privmsg'] > $userdata['user_lastvisit'] )
{
$sql = "UPDATE " . USERS_TABLE . "
SET user_last_privmsg = " . $userdata['user_lastvisit'] . "
WHERE user_id = " . $userdata['user_id'];
if ( !$db->sql_query($sql) )
{
message_die(GENERAL_ERROR, 'Could not update private message new/read time for user', '', __LINE__, __FILE__, $sql);
}

$s_privmsg_new = 1;
$icon_pm = $images['pm_new_msg'];
}
else
{
$s_privmsg_new = 0;
$icon_pm = $images['pm_no_new_msg'];
}

That part basically does the checking if you have a new message, and updates it so you don't get told more than once. The next piece of code you must use will enter in the variables of text so you can show later what the status of the PMs are.

}
else
{
$l_privmsgs_text = $lang['No_new_pm'];

$s_privmsg_new = 0;
$icon_pm = $images['pm_no_new_msg'];
}

if ( $userdata['user_unread_privmsg'] )
{
$l_message_unread = ( $userdata['user_unread_privmsg'] == 1 ) ? $lang['Unread_pm'] : $lang['Unread_pms'];
$l_privmsgs_text_unread = sprintf($l_message_unread, $userdata['user_unread_privmsg']);
}
else
{
$l_privmsgs_text_unread = $lang['No_unread_pm'];
}

Now, close that user logged in bracket, it is important or else no one will be able to anything else on your site with out being logged in.

} // if user logged in

In order to out put the data as text, you will want to add this line of code somewhere in the your site where you want it displayed:

<a href="<?php echo $phpbb_root_path2 ?>privmsg.php?folder=inbox"><?php echo $l_privmsgs_text ?></a>

Simple Eh? This next part is to add the capabilites of new PM Popup's when a new one arrives. You will need to add this code in your site's HTML head tags. It should be fine as it is:

<?php
if ( !empty($userdata['user_popup_pm']) ){
?>
<script language="Javascript" type="text/javascript">
<!--
if (<?php echo $s_privmsg_new ?>)
{
window.open('<?php echo $phpbb_root_path2 ."privmsg.".$phpEx."?mode=newpm" ?>', '_phpbbprivmsg', 'HEIGHT=225,resizable=yes,WIDTH=400');;
}
//-->
</script>
<?php
}
?>

4. Extending Birthday Announcments

This is written to add the Birthday announcements to the rest of your site, so it doesn't only pop-up on your forum. This will only work with the Birthday hack installed. See here for the hack by Niels Chr: http://www.phpbbhacks.com/viewhack.php?id=187 If you don't have this installed, skip this step, although, I highly recommend taking the time, and installing the birthdays hack.

Ok! So you've got the great birthday hack installed on your board? Well now, why not make the announcement pop-up on the main page, or any other page instead of just your forums? I don't see a reason why not, this is a very simple change like the Private Message one above it. Start with making sure the user is logged in:

if($userdata['session_logged_in']){ //logged in

This should all go around the same place as the Private Message hack, you can even use the same if user is logged in code if you want. This code is straight from the hack, and doesn't need any changing.

// see if user has or have had birthday, also see if greeting are enabled
$year=create_date('Y', time(), $board_config['default_timezone']);
if ($userdata['user_birthday']!=999999 && $board_config['birthday_greeting'] && create_date('Ymd', time(), $board_config['default_timezone']) >= $userdata['user_next_birthday_greeting'].realdate ('md',$userdata['user_birthday']))
{
$sql = "UPDATE " . USERS_TABLE . "
SET user_next_birthday_greeting = " . ($year+1) . "
WHERE user_id = " . $userdata['user_id'];
if( !$status = $db->sql_query($sql) )
{
message_die(GENERAL_ERROR, "Could not update next_birthday_greeting for user.", "", __LINE__, __FILE__, $sql);
}
$db->sql_freeresult($status);
$greeting_flag=1;
} else $greeting_flag=0;//Sorry user shall not have a greeting this year

Now close your if user logged in bracket.

}//user logged in

You will want to add this part, as it is the most important part, ok they are all equally important. But this makes the window pop-up if it is their birthday. Here is the code, add it under the private message pop-up in the HTML head tags.

<?php
if(date('Y') == $userdata['user_next_birthday_greeting']){
?>
<script language="Javascript" type="text/javascript">
<!--
var greeting_flag = <?php echo $greeting_flag ?>;

if( greeting_flag )
{
window.open('<?php echo $phpbb_root_path2 ?>privmsg.php?mode=birthday', '_phpbbprivmsg', 'HEIGHT=225,resizable=yes,WIDTH=400');
}
//-->
</script>
<?php
}

5. Logged In Information

This should be the final step in making the site work, really nicely. It will display to the user if they are logged in, and will display other pertent information. This is all up to you, and can go anywhere in the HTML you want, I won't really even give an example, because it is all a prefrence of choice.

Basically, the first thing you need to do is check if the user is logged in.

<?php
if($userdata['session_logged_in']){
?>

Then, just type that the user is logged in, in some friendly manner. Like: "Welcome To Site Name", "You Are Logged In!", "Welcome Back"... etc. It would then he good to tell them who they are logged in as, this code is very simple as well and is:

<?php echo $userdata[username] ?>

You can then add things like links to the profile and the logout and other features you may have for your site. I recommend a link to their own profile, and to logout. The correct ways to do each of these is:

<a href="<?php echo $phpbb_root_path2 ?>profile.php?mode=editprofile">Profile</a>
<a href="<?php echo $phpbb_root_path2 ?>login.php?logout=true">Logout</a>

You can now close out the if user is logged in function that we opened up at the start. Don't be done right now, you will still want to give the users a way to log-in into your site, and even register. Start with the else function, then place links to the places you want the un-logged-in people to go. Then close the "else" tag.

<?php
}else{
?>

<a href="<?php echo $phpbb_root_path2 ?>login.php">Login</a>
<a href="<?php echo $phpbb_root_path2 ?>profile.php?mode=register">Register</a>

<?php
}
?>

6. Final Code & Conclusion

Here is a text file of all the code that was used in this tutorial, it is literally cut and paste with some minor alterations. Please go through it, and check all the parts, as there are places that need to be changed for you.

There you go! That should be all you need for this tutorial, now your PHPbb portal is open to your imagination. Look forward to further tutorials and hacks written by me at PHPBBHacks.com, and my website AJQuick.com. Thank you for reading, and have fun!

-AJ Quick
aj@ajquick.com


©2001 - 2005 AJ Quick