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);
Richard Slater says
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.
Mr Kirkland says
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.
Nick says
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?
Russ says
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’;
?>
Russ says
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
Russ says
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’;
?>
lilferry says
Cheers!
To make kirkland’s code work: Replace all ” and ‘ with ” or ‘.
lilferry says
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 '
Daniel says
Thanks, works perfectly!
Johan says
Thanks a lot for this script!
I search this for a while!
Martin Sarsini says
Finally a simple solution. Thank you very much
the only thing I should suggest is to use exactly the phpBB3 specs
// get some functions from phpBB3
define(‘IN_PHPBB’, true);
$phpbb_root_path = ‘./forum/’; // change this to phpBB3 dir
$phpEx = substr(strrchr(__FILE__, ‘.’), 1);
include($phpbb_root_path . ‘common.’ . $phpEx);
// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup();
all the global variables you have set are not required
/* set scope for variables required later */
thanks again, works great!
Floydian says
This was very helpful to me!
I had some code from the phpbb3 forum that simply would not work. (de ja vu anyone?)
I added the list of globals you put in: i.e.,
global $phpbb_root_path;
global $phpEx;
global $db;
global $config;
global $user;
global $auth;
global $cache;
global $template;
And it made it work perfectly!
Note that you could add in the globals like this:
global $phpbb_root_path,$phpEx,$db,$config,$user,$auth,$cache,$template;
It’s just a lil cleaner 😀 IMHO
Thanks a lot for having posted this!
july says
It’s works, but only with table users.
Kartik says
Here is the complete working code
session_begin();
$auth->acl($user->data);
/* the file with the actual goodies */
include(“../phpbb3/includes/functions_user.php”);
$myFile = “smf_all_data.csv”;
$fh = fopen($myFile, ‘r’);
while($theData = fgets($fh))
{
$data=explode(“;”,$theData);
$username = $data[0];
$password = $data[5];
$email = $data[0].”@iitk.ac.in”;
$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 “$username finished”;
}
echo “finished”;
?>
dschibut says
I started this discussion to speak about public available web proxies:
Which are really anonymous?
Which can unblock facebook, myspace etc, in other words: are fresh ?
Which can you recommend?
Thanks for your help,
Dschibut
P.S.: In my land, the freedom of speech is somehow limited, please give me a hint, if you are not sure about your recommendation.
HSCharles says
I have a flash website
i’m looking for the script who shows google ads with flash.
how can i get it?
Ramon Fincken says
You don’t need all those globals and I noticed phpbb3 won’t check the email for syntax.
I’ve merged all coding mentioned above and put it here:
http://www.ramonfincken.com/82/PHP coding/[phpbb3][snipplet] Registering users from your own external script.html
leonni.b says
Well, thnaks for this code!
But I always get this error:
Incorrect integer value: ” for column ‘id’ at row 1
I’m pretty sure of the values into the $user array, but it seems to have a problem with a type of one var, isn’t it?
Can anyone help me?
Ageman20XX says
Hey,
I realize I’m a little late to the game (well, only about a month) but I just needed to import a bunch of users into phpBB3 from a completely unrelated application and this article and its comments were totally my saving grace. I just wanted to say thanks to everyone who contributed. ^_^
-Age
Milan Janačković says
Hey guys,
I think than your method is really great but it is a little bit complicated. There is no need for so many options, just put my file into the folder where your phpbb3 is, modify name, pass an email and it will start working in the second.
Because of the problem with quotes on this site I’ve transferred my version into the TXT file.
Please, be free to download it from this address:
http://virtual-studio.info/files/insert_user_phpbb3.txt
Bye!
Gaya says
Hey thnx!
You saved me a hell of time with this! Works like a charm.
Now users are automaticlly registered for the forums too =)
Grtz, Gaya
Matt Moeller says
Thanks Milan, your trimmed up version worked spot on. Thanks a ton for having this post it saved me hours of troubleshooting. The phpBB community didn’t have much to go on in regards to v3.
Gary Blum says
I’m trying to sync up the password between another website (built in ASP) and PHPBB.
The goal: if a user changes his password in the main site, we can call a function in PHP (through a redirect or AJAX) that resets the forums’ password in PHPBB.
Is this possible? If so, what function would I need to call?
(I should note that I have no PHP experience, so this will obviously be a challenge.)
acctman says
how do you do a check to make sure the sure name does not already exist?
acctman says
is tere a way to past variables to the $username, $password and $email from an include or require insert? i tried using session and its not accepting the user session info from my site
jay4linux says
I am getting the following error:
PHP Fatal error: Call to a member function session_begin() on a on-object in /var/www/htdocs/sponsors/includes/phpBB.inc on line 25
I thought it had something to do with the paths, but I have tried hardcoding those and it did not help. Any thoughts, here is the code:
session_begin();
$auth->acl($user->data);
$user->setup();
require($phpbb_root_path .’includes/functions_user.php’);
$username = ‘abc123’;
$password = ‘abc123’; // Do NOT encrypt !
$email = ‘abc123@betaclub.org’; // Please validate this email yourself ! Phpbb will accept non-valid emails.
// Do a check if username is allready there, same for email, otherwhise a nasty error will occur
$user_row = array(
‘username’ => $username,
‘user_password’ => md5($password), ‘user_email’ => $email,
‘group_id’ => 2, #Registered users group
‘user_timezone’ => ‘1.00’,
‘user_dst’ => 0,
‘user_lang’ => ‘en’,
‘user_type’ => ‘0’,
‘user_actkey’ => ”,
‘user_dateformat’ => ‘d M Y H:i’,
‘user_style’ => 1,
‘user_regdate’ => time(),
);
$phpbb_user_id = user_add($user_row);
echo “New user id = “.$phpbb_user_id;
?>
Randy Rowe says
This reply is to Ramon Fincken and the code that is posted there.
I don’t have a login to post a reply there so I’ll post it here.
After the line:
email = ’email_here’; // Please validate this email yourself ! Phpbb will accept non-valid emails.
Adding this code will make phpBB3 verify the items that were input:
$valid_email = validate_email($email);
if ($valid_email) {
echo “Your email address could not be used because “.$valid_email.”.n”;
exit;
} else {
$valid_password = validate_password($password);
if ($valid_password) {
echo “Your password could not be used because “.$valid_password.”.n”;
exit;
} else {
$valid_username = validate_username($username);
if ($validate_username) {
echo “Your username could not be used because “.$valid_password.”.n”;
exit;
}
}
}
Hope the quotes make it through ok. Thanks for the pointers to get this started.
Randy Rowe says
Oops, cut and paste error on the last echo. The variable should be $valid_username instead of $valid_password.
Greg B says
Is there a phpbb function to update a user password after it has been created?
Ceb P says
Look at the wiki for more infos:
http://wiki.phpbb.com/User_add
Claudio Walser says
Nice, this works perfect.
Another problem i have is, if my user change the password in my external script. Has anyone a solution for changing the password in phpBB too?
Promi says
I have the above code, but something does not work pls help me
define(‘IN_PHPBB’, true);
global $config, $db, $user, $auth, $template, $phpbb_root_path, $phpEx;
/* the file with the actual goodies */
$phpEx = substr(strrchr(__FILE__, “.”), 1);
$phpbb_root_path = “phpBB3/”;
include($phpbb_root_path . “includes/functions_user.” . $phpEx);
include($phpbb_root_path . “includes/functions.” . $phpEx);
include($phpbb_root_path . “common.” . $phpEx);
// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup();
$username = “proba”;
$password = “proba”;
$email = “email@eml.hu”;
$user_inactive_reason = 0;
$user_inactive_time = 0;
$user_type = USER_NORMAL;
$user_row = array(
“username” => $username,
“user_password” => phpbb_hash($password),
“user_email” => $email,
“group_id” => 2, #Registered users group
“user_timezone” => 1.00,
“user_dst” => 1,
“user_lang” => “hu”,
“user_type” => $user_type,
“user_actkey” => ”,
“user_dateformat” => “D M d, Y g:i a”,
“user_style” => 1,
“user_regdate” => time(),
“user_inactive_reason” => $user_inactive_reason,
“user_inactive_time” => $user_inactive_time,
);
/* Now Register user */
$phpbb_user_id = user_add($user_row);
echo “finished”;
Promi says
It writes the following error:
The config. file could not be found.
Click here to install phpBB
Promi says
This is the latest version and the browser cannot show the page (HTTP500):
define(‘IN_PHPBB’, true);
session_start();
global $config, $db, $user, $auth, $template, $phpbb_root_path, $phpEx;
/* the file with the actual goodies */
$phpbb_root_path = (defined(‘PHPBB_ROOT_PATH’)) ? PHPBB_ROOT_PATH : ‘./’;
$phpEx = substr(strrchr(__FILE__, ‘.’), 1);
include($phpbb_root_path . “includes/functions_user.” . $phpEx);
include($phpbb_root_path . “includes/functions.” . $phpEx);
include($phpbb_root_path . “common.” . $phpEx);
// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup();
$username = “proba”;
$password = “proba”;
$email = “email@eml.hu”;
$user_inactive_reason = 0;
$user_inactive_time = 0;
$user_type = USER_NORMAL;
$user_row = array(
“username” => $username,
“user_password” => phpbb_hash($password),
“user_email” => $email,
“group_id” => 2, #Registered users group
“user_timezone” => 1.00,
“user_dst” => 1,
“user_lang” => “hu”,
“user_type” => $user_type,
“user_actkey” => ”,
“user_dateformat” => “D M d, Y g:i a”,
“user_style” => 1,
“user_regdate” => time(),
“user_inactive_reason” => $user_inactive_reason,
“user_inactive_time” => $user_inactive_time,
);
/* Now Register user */
$phpbb_user_id = user_add($user_row);
echo “finished”;
Promi says
The problem solved:
I removed the
$phpbb_root_path = (defined(’PHPBB_ROOT_PATH’)) ? PHPBB_ROOT_PATH : ‘./’;
part from the code
Promi says
and added a simple string instead:
$phpbb_root_path = ‘phpBB3/’;
WebVeins.in says
I am already using the same code. Everything is working fine. But it does not send any confirmation email to the registering user. But when i try to send register from phpBB, it does. What should I do. Please help me get out of this situation.
Mario says
Very Useful, tnx a lot!
ganesh says
Where i paste that code. I need help from you
SangPetualang says
i mix some codes given by all comment here ..
this work for me:
// get some functions from phpBB3
define(‘IN_PHPBB’, true);
$phpbb_root_path = ‘../phpBB/’;
$phpEx = substr(strrchr(__FILE__, ‘.’), 1);
include($phpbb_root_path . ‘common.’ . $phpEx);
// Start session management
$user->session_begin();
$auth->acl($user->data);
include($phpbb_root_path .’includes/functions_user.php’);
//$user->setup();
$user_row = array(
‘username’ => ‘testtest’,
‘user_password’ => md5(‘test’),
‘user_email’ => ‘test@test.com’,
‘group_id’ => 2,
‘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 $phpbb_user_id.’ finished’;
Mark P says
the question is regarding here ‘user_actkey’ => ”, using 1 double quote? or 2 single quote please clear this up, because it has no value indicated. Thanks
Alex says
Hello SangPetualang,
I tried your modified code and it works fine for me. Thank you to all here.
kevin says
i using this script and it works but there is one trouble for the password integration … it hashed like phpbb_hash do but when i want to login i wont reconize my pass
its like phpbb_hash makes a random salt in the password
Greetings , kevin