Using phpBB with another login system

This forum is now closed as part of retiring phpBB2.
Forum rules
READ: phpBB.com Board-Wide Rules and Regulations

This forum is now closed due to phpBB2.0 being retired.
User avatar
pyrokenesis
Registered User
Posts: 75
Joined: Sat Dec 23, 2006 1:13 pm
Location: London

Post by pyrokenesis » Tue Mar 06, 2007 10:57 am

pyrokenesis wrote: Does anyone know how to redirect to a page of your choice when logging out (i.e. the referrer), as at the moment you are redirected to the forum index page?


Any takers? :)

User avatar
pyrokenesis
Registered User
Posts: 75
Joined: Sat Dec 23, 2006 1:13 pm
Location: London

Post by pyrokenesis » Tue Mar 06, 2007 1:12 pm

Dont worri, me founded it, yup mi did... :roll:

User avatar
brainwipe
Registered User
Posts: 4
Joined: Wed Nov 12, 2003 1:50 pm
Location: UK

Re: Using phpBB with another login system

Post by brainwipe » Mon Mar 26, 2007 11:36 am

Pyro, I just started trying to solve this very problem when I came across and read through your investigation. It works smashingly. I can't thank you enough! If I were not already married, I'd be down on one knee now.

The smallest about of stuff you actually need to check to see if the user is logged in is:

Code: Select all

<?php
    // Change the below for your root
    $phpbb_root_path = '../forum/';
                      
    define('IN_PHPBB', true);

    //
    // phpBB related files
    //

    include($phpbb_root_path . 'extension.inc');
    include($phpbb_root_path . 'common.' . $phpEx);
    include($phpbb_root_path . 'includes/bbcode.' . $phpEx);

    //
    // start session management
    //

    $userdata = session_pagestart($user_ip, PAGE_INDEX);
    init_userprefs($userdata);
       
    if($userdata['session_logged_in'])
    {
		// Do something because the user is logged in.
    }
    else
    {
	        // User not logged in, so do something else. Perhaps weep into pillow. Or offer a login box.
    }

User avatar
brainwipe
Registered User
Posts: 4
Joined: Wed Nov 12, 2003 1:50 pm
Location: UK

Re: Using phpBB with another login system

Post by brainwipe » Mon Mar 26, 2007 11:37 am

Pyro, I just started trying to solve this very problem when I came across and read through your investigation. It works smashingly. I can't thank you enough! If I were not already married, I'd be down on one knee now.

The smallest about of stuff you actually need to check to see if the user is logged in is:

Code: Select all

<?php
    // Change the below for your root
    $phpbb_root_path = '../forum/';
                      
    define('IN_PHPBB', true);

    //
    // phpBB related files
    //

    include($phpbb_root_path . 'extension.inc');
    include($phpbb_root_path . 'common.' . $phpEx);
    include($phpbb_root_path . 'includes/bbcode.' . $phpEx);

    //
    // start session management
    //

    $userdata = session_pagestart($user_ip, PAGE_INDEX);
    init_userprefs($userdata);
       
    if($userdata['session_logged_in'])
    {
		// Do something because the user is logged in.
    }
    else
    {
	        // User not logged in, so do something else. Perhaps weep into pillow. Or offer a login box.
    }
?>

User avatar
pyrokenesis
Registered User
Posts: 75
Joined: Sat Dec 23, 2006 1:13 pm
Location: London

Re: Using phpBB with another login system

Post by pyrokenesis » Mon Mar 26, 2007 2:56 pm

:D No worries mate.

The reason it took me so long originally was because I was trying to do it the other way around and use my session management to start phpBB. It's so much easier this way though, I'm glad it helped.
aka Bigglesworth aka Billy Ray Valentine - Capricorn
http://www.royalurbanite.com/automp3mod/

henryrhenryr
Registered User
Posts: 1
Joined: Fri Apr 06, 2007 2:36 pm

Re: Using phpBB with another login system

Post by henryrhenryr » Fri Apr 06, 2007 3:09 pm

Just been giving this a try. Here's my efforts so far - perhaps helpful for some other people although I'm not a php expert so the code could be a bit dodgy.

1. I added a column on my mainsite user table to store the forum_user_id.

2. I added some code to sessions.php (below). This code takes the session info from the mainsite login when you access the forum, looks up the forum_user_id (using new column (1)), then uses the session_begin() function in sessions.php to create a phpBB session. Then of course phpBB thinks you're logged in - the $userdata array is fully populated.

==> Only edits to phpBB files are:

1. Add details to allow the phpBB files to get the session info on load (I am using $_SESSION array).

2. Inside sessions.php:

a. session_begin() function:

Code: Select all

global mainsite_sid, mainsite_userid;
Insert the following as the first condition on the first conditionals (making the original one an elseif).

Code: Select all

if (isset($mainsite_id) && is_numeric($mainsite_id))
    {
        $sql= "SELECT forum_user_id FROM mainsite_user_table WHERE mainsite_user_id=".(int)$mainsite_id;
        if ( $result = $db->sql_query($sql) ) {
          $row= $db->sql_fetchrow($result);
          $sessiondata= array('autologinid'=>'', 'userid'=>$a_row['forum_user_id_fk']);
        }
        else { $sessiondata= array(); }
        $session_id= $mainsite_sid; /**/
        $session_method= SESSION_METHOD_COOKIE; /**/
    }
NOTE: I'm not actually sure if /**/ line is required but seems to work.

Then in the session_pagestart() function - something very similar:

Code: Select all

global mainsite_sid, mainsite_userid;
This looks up the forum_user_id from the mainsite table (as above). Then sets $sessiondata. Then gets the session_begin function to return $userdata. The last bit gets the session id out of the $userdata just generated by session_begin or else uses the mainsite one - not sure if this helps or not - thought - why not?!

Code: Select all

if (isset($mainsite_id) && is_numeric($mainsite_id))
    {
        $sql= "SELECT forum_user_id FROM mainsite_user_table WHERE mainsite_user_id=".(int)$mainsite_id;
        if ( $result = $db->sql_query($sql) ) {
          $a_row= $db->sql_fetchrow($result);
          $sessiondata= array('autologinid'=>'', 'userid'=>$row['forum_user_id']);
          $userdata = session_begin($a_row['forum_user_id'], $user_ip, $thispage_id);
        }
        else { $sessiondata= array(); }
        $session_id = isset ($userdata['session_id']) ? $userdata['session_id'] : $mainsite_sid;
    }

So that seems to work for me. I've just managed to make it work - it may not be the best solution but perhaps it will help if you're stuck.

ps maybe some typos in code as I changed a few variable names on the post to make clearer...

ch8rt
Registered User
Posts: 16
Joined: Wed Dec 13, 2006 11:49 am
Location: UK
Contact:

Re: Using phpBB with another login system

Post by ch8rt » Thu May 17, 2007 12:11 pm

An interesting read. I really don't understand any of this stuff. Does anyone know if this would work with any front end? I've just installed Elgg and we're using it as our front end, but currently users have had to register for both the front and the forum? And similarly have to log in each time.

Anyone willing to explain to someone new to all this will get a virtual cookie.

flagman5
Registered User
Posts: 11
Joined: Sat May 19, 2007 1:44 am

Re: Using phpBB with another login system

Post by flagman5 » Mon May 21, 2007 3:21 am

i have sucessfully inserted a record into the phpbb_sessions table, and when i visit the forum after logging in from my mainsite, i see the user is shown as logged in on the index page, but the login boxes are still there, so its like a ghosting effect. kind of like what pyro had i think?

someone needs to explain exactly what phpbb looks for to see if the current user is logged in or not. apparently just inserting a record in sessions does not work...phpbb is looking for something else on top of the record in the session, what is it??

isnt this a majorly common problem with phpbb? to integrate it with the mainsite logins...

flagman5
Registered User
Posts: 11
Joined: Sat May 19, 2007 1:44 am

Re: Using phpBB with another login system

Post by flagman5 » Mon May 21, 2007 5:30 am

ok....so now i also set the cookies that sessions_begin set for phpbb, and STILL the page does not recognize the user has logged in ALTHOUGH it shows on the users online list that the user is logged in.... oy...

flagman5
Registered User
Posts: 11
Joined: Sat May 19, 2007 1:44 am

Re: Using phpBB with another login system

Post by flagman5 » Mon May 21, 2007 6:07 am

i got it!!!!!! my mistake was that i wasnt making the cookie available to the rest of the domain! doh!

so here is the process for all future webdevelopers trying to do this:

This is ONLY for you to want to keep your own login system on your mainsite, and you want to be able to login from the mainsite, and then go directly into the forums without logging in again.

All the modification is INSIDE your mainsite login.php, not the phpbb one.

1. modify your own log-in file to create a session by almost mimicking sessions_begin() function in sessions.php. you need to generate a session_id in a similar fashion as phpbb, and encode the uesr_ip in the same fashion as well. i hardcoded most of the other options. and then insert this into the sessions table in mysql db.

2. then create your own $userdata, you will need this to set 2 cookies . one is SID one is DATA. just follow sessions_begin() function as well. MAKE SURE YOU SETCOOKIE WITH THE OPTION TO MAKE IT AVAILABLE TO SUBDOMAINS (if you are using forums.url.com) <--- this took me forever to figure out! ><

thats it! then you should be set.
good luck to the rest of ya'll, i am outssss

ch8rt
Registered User
Posts: 16
Joined: Wed Dec 13, 2006 11:49 am
Location: UK
Contact:

Re: Using phpBB with another login system

Post by ch8rt » Mon May 21, 2007 8:09 am

Is anyone willing to explain this to someone who has never done this kind of thing before. Also am I right in thinking that phpbb uses id numbers rather than user name, meaning new members would have to somehow be added in manually? Or would someone registering to both the forum and the mainsite with the same username automatically be able to do this?

User avatar
pyrokenesis
Registered User
Posts: 75
Joined: Sat Dec 23, 2006 1:13 pm
Location: London

Re: Using phpBB with another login system

Post by pyrokenesis » Mon May 21, 2007 7:52 pm

Good contribution flagman5, I'll investigate your method at some point. As you may have read above, I abandoned that way and logged in via phpbb. But I still like my login system so I'm defo gonna check it out.

:D
aka Bigglesworth aka Billy Ray Valentine - Capricorn
http://www.royalurbanite.com/automp3mod/

User avatar
pyrokenesis
Registered User
Posts: 75
Joined: Sat Dec 23, 2006 1:13 pm
Location: London

Re: Using phpBB with another login system

Post by pyrokenesis » Mon May 21, 2007 8:04 pm

ch8rt wrote:Is anyone willing to explain this to someone who has never done this kind of thing before. Also am I right in thinking that phpbb uses id numbers rather than user name, meaning new members would have to somehow be added in manually? Or would someone registering to both the forum and the mainsite with the same username automatically be able to do this?
What do you need to know? Specific questions are more helpful.

PHPBB does use user_id's as the primary key as opposed to the username, like some other scripts.

The id is populated in phpbb 2.x.x by a little mathematics. The rows are counted and then once the number of existing users is retrieved, 'one' is added to the result. This gives you the new id. So all you need to do is insert into the phpbb user table the id, username, email etc. in the same place that it is done in your main script.

Thus, at registration:

- insert your new user (as normal)
- query phpbb_user table and retrieve number of users
- add one to result
- insert into phpbb_users the new user from your main site with the newly generated id

And Bob's your Uncle, or possibly your Aunt... ;)
aka Bigglesworth aka Billy Ray Valentine - Capricorn
http://www.royalurbanite.com/automp3mod/

User avatar
Brf
Support Team Member
Support Team Member
Posts: 50746
Joined: Tue May 10, 2005 7:47 pm
Location: {postrow.POSTER_FROM}
Contact:

Re: Using phpBB with another login system

Post by Brf » Mon May 21, 2007 8:25 pm

No. You cannot just add a new user into phpbb_users table. You also have to add that user into the groups and user_group tables, or that user's permissions will be screwed up.

User avatar
pyrokenesis
Registered User
Posts: 75
Joined: Sat Dec 23, 2006 1:13 pm
Location: London

Re: Using phpBB with another login system

Post by pyrokenesis » Mon May 21, 2007 9:45 pm

:oops:

Yep I forgot about that. I learned that the hard way.

Anything that is done at registration for the phpbb forum, must be done @ registration on your main site, then you'll be sorted.
aka Bigglesworth aka Billy Ray Valentine - Capricorn
http://www.royalurbanite.com/automp3mod/

Post Reply

Return to “[2.0.x] MOD Writers Discussion”

Who is online

Users browsing this forum: No registered users and 4 guests

cron