Nov 04

Automated member registration for phpBB

A lot of websites may require phpBB plugged-in as an add-on to the main site. In such a case if the main website already has a membership module in place, then it is desirable that when a member signups on the main website, he also becomes a member in phpBB.

There are 3 ways of doing this:

  1. Manually redirect the user to the phpbb signup form after signing up on the main site
  2. Do an automated form post in code to the phpbb registration page
  3. Add data to the phpbb database directly thus bypassing all the phpbb code
  4. Use phpbb functions to add the user to the phpbb database

Method 1 is cumbersome and messy as the user is forced to enter his details twice- once for the main site and then for phpbb. This is not a very good option as you are making the user do all the dirty work for you.

Method 2 is not possible with the latest version of phpBB (3.0 Olympus) as it requires Captcha authentication, unless you have a way of hacking the captcha code

Method 3 is very undesirable as there are around 4 tables and 60 fields to update when a member signs up. Besides some of the fields like password etc are hashed using code internal to phpBb.

Method 4 is the most seamless and safe.

It just needs 3 fields to be prefilled: username, user_password and user_email . The rest of the fields are filled with default values by phpBB.

The code is given below:



<div style="font-family: courier; color:black; font-size: 10px; background-color: #ffffd5; overflow-x: scroll;">define('IN_PHPBB', true);
define('ROOT_PATH', "../pathtoforums");

if (!defined('IN_PHPBB') || !defined('ROOT_PATH')) {
//exit();
}
else {
$phpEx = "php";
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : ROOT_PATH . '/';
include($phpbb_root_path . 'common.' . $phpEx);
include($phpbb_root_path . 'includes/functions_user.' . $phpEx);
$arrTime = getdate();
$unixTime = strtotime($arrTime['year'] . "-" . $arrTime['mon'] . '-' . $arrTime['mday'] . " " . $arrTime['hours'] . ":" . $arrTime['minutes'] . ":" . $arrTime['seconds']);

$user_row = array(
'username'              =&gt; $_GET['username'],
'user_password'         =&gt; phpbb_hash($_GET['pwd']),
'user_email'            =&gt; $_GET['email'],
'group_id'              =&gt; (int) 2,
'user_timezone'         =&gt; (float) 0,
'user_dst'              =&gt; "0",
'user_lang'             =&gt; "en",
'user_type'             =&gt; 0,
'user_actkey'           =&gt; "",
'user_ip'               =&gt; $_SERVER['REMOTE_HOST'],
'user_regdate'          =&gt; $unixTime,
'user_inactive_reason'  =&gt; 0,
'user_inactive_time'    =&gt; 0
);

// all the information has been compiled, add the user
// tables affected: users table, profile_fields_data table, groups table, and config table.
$user_id = user_add($user_row);                    }</div>


It is assumed that the user data is being passed to this page via a GET. The most important thing is the define(ROOT_PATH) directive-it should point to the base directory of phpbb. Eg.if it is installed in /phpbb then ROOT_PATH becomes /phpbb.One thing to remember is that if the username is a duplicate one, then phpbb stops execution and shows the error page. Of course, the easiest way around this is the main site’s membership module which is taking care of userid validation, only then would the phpbb code called so its unlikely you will get a duplicate member error.

 

Some pitfalls to avoid :

  • If you integrate the code with other code in a page, you might get a class conflict error with ‘class user’ . If you already have a class called user in your main code then it will conflict with phpbb’s user class.
  • If you are directly opening a database connection in a page and then calling the above code in the same page, then you may get some runtime exceptions as it conflicts with the $db class of phpbb. The way around is this to capture the data in variables and then close and dispose all database classes before calling phpbb code.