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:
- Manually redirect the user to the phpbb signup form after signing up on the main site
- Do an automated form post in code to the phpbb registration page
- Add data to the phpbb database directly thus bypassing all the phpbb code
- 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:
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' => $_GET['username'], 'user_password' => phpbb_hash($_GET['pwd']), 'user_email' => $_GET['email'], 'group_id' => (int) 2, 'user_timezone' => (float) 0, 'user_dst' => "0", 'user_lang' => "en", 'user_type' => 0, 'user_actkey' => "", 'user_ip' => $_SERVER['REMOTE_HOST'], 'user_regdate' => $unixTime, 'user_inactive_reason' => 0, 'user_inactive_time' => 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); }
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.
What PHPBB file are you adding this into?
Is this a section of the database you have to altar, or a completely different file? I’ve been trying to find it for ages!
This is a new file which you create. You include the required phpbb files : common.php and function_user.php .
The main code is user_add() which is a phpbb function found in function_user.php
No database is accessed or altered directly
Fatal error: Call to a member function sql_query() on a non-object in /home/…/Forum/includes/cache.php on line 51
That’s when I’m calling the above code in a standalone file. No other code involved. How can I get the DB layer working before all this?
Todor:
It looks like $db object is not instantiated or is not accessible. You will have to check your code to make sure, the $db variable is instantiated and is the correct object type.
Thank you for this code! I have been looking for this sort of solution for a long time.
I do have one question about the code, though. I am getting the same error as Todor, and I don’t know how to fix it. How do I instantiate the $db variable and make sure it is the correct object type? I guess I don’t understand how to implement this solution in the context of this code.
Bob: The db object for phpbb is created in /common.php which is in the root folder of phpbb.
You have to assign the database connection vars for the $db object to get created. The connection details are set in /config.php in the phpbb folder.
I hope this helps you locate the problem.
Amit, thank you for the explanation. That part of the code seems to be working fine now.
My only problem now seems to be in this section of the code:
define(’IN_PHPBB’, true);
define(’ROOT_PATH’, “/phpbb”);
if (!defined(’IN_PHPBB’) || !defined(’ROOT_PATH’)) {
//exit();
}
The program always exits. I tried taking out either ends of the if statement to see which variable was not defined, but both times the program exited. Do you know what might be going on? I’m very puzzled.
Bob,
It is unlikely that the if (!defined(… statement is executing since we are defining IN_PHPBB and ROOT_PATH just above it. To make sure the defines are working, just print out the values. I think some other error is happening at runtime. If you have error_reporting(E_ALL) at the top of the file it will surely show where its getting stuck.
Hope that helps.
Hi!
Thank you for your code. Now I’m trying to put it right…I didn’t understand where I have to put it.In my workspace or in root phpbb? And how can I make a connection between this…
Thank you
Denisa,
You can put the code anywhere in the site. Just make sure the define(‘ROOT_PATH’) points to the root folder of the phpbb application. In other words the ROOT_PATH should point to the root folder of PHPBB
Thank you
As you say I put it in root folder of PHPBB, and now I include my new file in the file where I add the user in my site. Isn’t it? In some moments I can say if it works because I’m trying right now:) It’s very nice of you to be online
It works fine, but it seems that it doesn’t recognize my password. I send it using GET,in the phpbb_users table it is present, but when I want to login in forum it says that the password is incorrect. If I say I forget my password, they send another password and the change is also in my phpbb_users table.With the new password I can login into forum. I think i make a mistake …..
Denisa,
Probably phpbb is checking some other field also to see if the login is valid. You will have to check the phpbb login code to see the reason why its rejecting your initial password.
Thank you so very much for this, it worked great!
I had that $db issue like everyone else but it was quickly (1 hour) remedied by putting this atop my function…
global $phpbb_root_path, $phpEx, $user, $db, $config, $cache, $template;
Once again, thank you!
Jeff,
I think I should put the global declaration as part of the code snippet. It would save people a lot of trouble.
Thanks for the input.
I had that $db issue like everyone else but it was quickly (1 hour) remedied by putting this atop my function…
_________________________
Imran