So working on a phpbb2 -> phpbb3 upgrade this weekend and the final part was updating the script to sync my applications user table with phpbb3, so users of my application automatically have an account on the forum with the same credentials.
phpbb3 has a new password handling system so it’s not so simple to write raw queries to insert/update the phpbb database. Instead, after a little research I opted to use phpbb’s own functions to add a new user. As noted here and here you can call the user_add function from includes/functions_user.php. This works fine until you want to call the function from inside another function as noted on the above links, it’s a little tricky getting it to work.
I managed to come up with a fairly simple solution - you need to declare a number of key variables as global at the top of your function so they are in scope when you include the phpbb files.
define(’IN_PHPBB’, true);
/* set scope for variables required later */
global $phpbb_root_path;
global $phpEx;
global $db;
global $config;
global $user;
global $auth;
global $cache;
global $template;# your php extension
$phpEx = substr(strrchr(__FILE__, ‘.’), 1);
$phpbb_root_path =; /* includes all the libraries etc. required */
require($phpbb_root_path .”common.php”);
$user->session_begin();
$auth->acl($user->data);/* the file with the actual goodies */
require($phpbb_root_path .”includes/functions_user.php”);/* All the user data (I think you can set other database fields aswell, these seem to be required )*/
$user_row = array(
‘username’ => “Username”,
‘user_password’ => md5(”Password”), ‘user_email’ => “Email”,
‘group_id’ => $default_group_id,
‘user_timezone’ => ‘1.00′,
‘user_dst’ => 0,
‘user_lang’ => ‘en’,
‘user_type’ => ‘0′,
‘user_actkey’ => ”,
‘user_dateformat’ => ‘d M Y H:i’,
‘user_style’ => $not_sure_what_this_is,
‘user_regdate’ => time(),
);/* Now Register user */
$phpbb_user_id = user_add($user_row);

have you had any experience with exception handling from the add_user() function, for example if a user tries to register again the add_user() function dies out with a “General Error” message.
Hi Richard,
Sorry I’ve not had that particular situation so I can’t comment.
I did notice that a side effect of including all the phpbb code is the error reporting, e.g. it will start reporting errors (as per phpbb settings) for any code from the point you include the phpbb3 code. I then uncommented @define(’DEBUG’, false); from config.php as it was interfering with my own error reporting i.e. it was diplaying errors to the end user rather than logging them silently as in my application.
I’ve tried using your code (and others from the phpbb Mod forum) without any luck. My user_row array is correctly filled out, but the user_add() function does nothing - doesn’t insert anything to the database, doesn’t return a user ID, doesn’t even spit out an error.
Any suggestions?
I tried the code above with no success, so I hacked it around for a while as below. It retrieves a list of users from a MySQL table called USERS which has 3 fields; USERNAME, PASSWORD, EMAIL.
This works fine for me on PHPBB 3. FTPed as addusers.php to my forum directory and activated with HTTP://www.domain.com/forum/addusers.php.
——————————————————-
#addusers.php
session_begin();
$auth->acl($user->data);
/* the file with the actual goodies */
include($phpbb_root_path .’includes/functions_user.php’);
$result = mysql_pconnect(’server’, ‘database’, ‘password’);
$result = mysql_select_db(’database’);
$query = “select * from users”;
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result))
{
$username = $row['username'];
$password = $row['password'];
$email = $row['email'];
$user_row = array(
‘username’ => $username,
‘user_password’ => md5($password),
‘user_email’ => $email,
‘group_id’ => 2, #Registered users group
‘user_timezone’ => 0,
‘user_dst’ => 1,
‘user_lang’ => ‘en’,
‘user_type’ => 0,
‘user_actkey’ => ”,
‘user_dateformat’ => ‘D M d, Y g:i a’,
‘user_style’ => 1,
‘user_regdate’ => time(),
);
/* Now Register user */
$phpbb_user_id = user_add($user_row);
}
echo ‘finished’;
?>
If the above code doesn’t work then try replacing the single quotes in the code. I suspect these are changed when uploading to this message board. It was one of the main problems with the original code above.
Cheers
Russ
Looks like I missed the first part of the code when copying. I’ve listed full program below. Sorry about that.
session_begin();
$auth->acl($user->data);
/* the file with the actual goodies */
include($phpbb_root_path .’includes/functions_user.php’);
$result = mysql_pconnect(’server’, ‘database’, ‘password’);
$result = mysql_select_db(’database’);
$query = “select * from users”;
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result))
{
$username = $row['username'];
$password = $row['password'];
$email = $row['email']
$user_row = array(
‘username’ => $username,
‘user_password’ => md5($password),
‘user_email’ => $email,
‘group_id’ => 2, #Registered users group
‘user_timezone’ => 0,
‘user_dst’ => 1,
‘user_lang’ => ‘en’,
‘user_type’ => 0,
‘user_actkey’ => ”,
‘user_dateformat’ => ‘D M d, Y g:i a’,
‘user_style’ => 1,
‘user_regdate’ => time(),
);
/* Now Register user */
$phpbb_user_id = user_add($user_row);
}
echo ‘finished’;
?>
Cheers!
To make kirkland’s code work: Replace all †and ‘ with ” or ‘.
oh hell. Disregard my above post, the post script on this site is filtering away the signs I wrote. Let me specify again:
Replace all †and ‘ with " or '
Thanks, works perfectly!