Well, here I was again doing a fast freelance job for a friend - He wanted a full functioning forum up and ready to go as fast as possible. I of course thought of the very nice free system - phpbb(http://phpbb.com). To make things interesting we had already put in place a custom made membership system, the phpbb forum would now have to integrate into our Registration and Login process seamlessly.
My first instinct was to go to the boards on PHPBB and do a quick search to find my solution - It turns out the solution is not fully laid out... Or atleast to me it didn't seem to be layed out.
So after about 30-45 minutes looking over threads and replies I decided to just look at how PHPBB initiated its sessions and cookies and go for it myself.
Here are my easy steps to accomplish this login / registration task (I have no error checking/hack prevention in place for my example, I don't want to do everything for you now
)
STEP 1 - REGISTRATION
-
//Query to select the max number of users in the table
-
$sql = "SELECT MAX(user_id) AS total FROM phpbb_users";
-
$select=mysql_query($sql);
-
$row=mysql_fetch_array($select);
-
-
//Taking the max number of records and adding 1 for the next user_id
-
$user_id=$row['total']+1;
-
-
//Query to insert the basics into the users table.
-
//NOTE - Take validation into your own hands, this is just an example.
-
$sql = "INSERT INTO phpbb_users (user_id, username, user_regdate,
-
user_password, user_email,user_active)
-
VALUES ($user_id, '$username',".time().",'$password','$email','1')";
-
-
//Insert the user
-
$insert=mysql_query($sql);
-
-
//Setup the users group so he/she has posting ability.
-
//Why group_id=3?
-
//On a basic PHPBB installation:
-
// ID #1 = Visitor/Guest
-
// ID #2 = Admin
-
// ID #3 = Basic registered user
-
//If you have a custom user group setup,
-
//replace 3 with the id number.
-
$group_id=3;
-
-
$sql = "INSERT INTO phpbb_user_group (user_id,
-
group_id, user_pending)
-
VALUES ($user_id, $group_id, 0)";
-
-
//Insert user into the correct group.
-
$insert=mysql_query($sql);
-
-
//Add in your own registration for your own website here.
STEP 2 - LOGIN
-
//Place this on top of your login script
-
-
define("IN_LOGIN", true);
-
define('IN_PHPBB', true);
-
-
//Be sure to update the root path to match your forum settings
-
$phpbb_root_path = './forum/';
-
include($phpbb_root_path . 'extension.inc');
-
include($phpbb_root_path . 'common.'.$phpEx);
While checking for the username/password within your own system (and succeeding) enter these few lines of code.
-
$select=mysql_query("SELECT * FROM phpbb_users WHERE username='".$username."'");
-
$row=mysql_fetch_array($select);
-
$userdata = session_pagestart($user_ip, PAGE_LOGIN);
-
$session_id = session_begin($row['user_id'],
-
$user_ip, PAGE_INDEX, FALSE, $autologin, '0');
The above peace of code will use PHPBB's own session handler to create the cookies / sessions needed to log the user into the forum - as well as your own system.
Enjoy!