CMSMS & PHPBB Forum Session Sharing

Do something cool with CMS? Show us ...
This board is for 'Answers', and the discussion of answers... Not for questions.
Post Reply
magallo
Forum Members
Forum Members
Posts: 99
Joined: Thu Mar 24, 2011 12:37 am

CMSMS & PHPBB Forum Session Sharing

Post by magallo »

Hi guys i was building a project where the main domain shares the same header menu with a phpbb forum located at a subdomain. Similar to cmsms site and forum.

CMSMS v2.2.12
PHPBB v3.2.7

phpbb has some tutorials on how to get the user session and display it into a template:

Code: Select all

define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : '../';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);
$user->session_begin();
$auth->acl($user->data);
$user->setup();
global $template;
page_header('Login Bar');
$template->set_filenames(array(
    'body' => 'top_bar.tpl',
)); 
//print_r($user->data);
page_footer();
The problem i was facing was functions conflict when "include" in my custom function or in my custom module.

The way i worked around this problem was getting the user's cookie and creating my own database query:

Code: Select all

$cookie = $_COOKIE["v2_btg_forum_sid"];
if( !$cookie ) exit;
$db = new conn_forum; //database connection to your forum
$db->connect();
$sql = "SELECT s.session_id, s.session_ip, s.session_user_id, u.user_type, u.username, u.user_last_privmsg FROM phpbb_sessions s 
LEFT JOIN phpbb_users u ON s.session_user_id = u.user_id
WHERE session_id = '".$cookie."'";
$result=$db->query($sql);

    while($row = mysqli_fetch_assoc($result)){
        $privmsgs = privmsgs($db,$row['session_user_id']);
        $session = array(
                            'session_id'=> $row['session_id'], 
                            'session_user_id'=> $row['session_user_id'],
                            'username'=> $row['username'],
                            'session_ip'=> $row['session_ip'],
                            'pm_new' => $privmsgs['pm_new'],
                            'pm_unread' => $privmsgs['pm_unread'],
                            'user_type' => $row['user_type']
                              );
    }

if($session['session_user_id'] > 1){
    $tpl = $smarty->CreateTemplate($this->GetTemplateResource('login_bar.tpl'),null,null,$smarty);
    $tpl->assign('session',$session);

    $tpl->assign('U_PROFILE', 'https://'.FORUM_DOMAIN . '/ucp.php');
    $tpl->assign('U_BOOKMARK', 'https://'.FORUM_DOMAIN . '/ucp.php?i=main&mode=bookmarks');
    $tpl->assign('U_LOGIN_LOGOUT', 'https://'.FORUM_DOMAIN . '/ucp.php?mode=logout&sid='.$cookie);
    $tpl->assign('U_PRIVATEMSGS', 'https://'.FORUM_DOMAIN . '/ucp.php?i=pm&folder=inbox');

    if($session['pm_unread'] > 0) {
        $tpl->assign('PRIVATE_MESSAGE_COUNT',$session['pm_unread']);
    }

    $tpl->display();
    //echo json_encode($session);
}

function privmsgs($db,$uid){
    $sql = "SELECT sum(pm_new) AS pm_new,sum(pm_unread) AS pm_unread FROM phpbb_privmsgs_to WHERE user_id = $uid";
    $result=$db->query($sql);
    while($row = mysqli_fetch_assoc($result)){
        $array = array('pm_new'=> $row['pm_new'], 'pm_unread'=> $row['pm_unread']);
    }
    return $array;
}


This will provide you with the data, you then need to attache the login_bar.tpl template and process the variables.
Hope this helps.
Post Reply

Return to “Tips and Tricks”