Manually insert users into PHPBB3

Need some custom code changes to the phpBB core simple enough that you feel doesn't require an extension? Then post your request here so that community members can provide some assistance.

NOTE: NO OFFICIAL SUPPORT IS PROVIDED IN THIS SUB-FORUM
Forum rules
READ: phpBB.com Board-Wide Rules and Regulations

NOTE: NO OFFICIAL SUPPORT IS PROVIDED IN THIS SUB-FORUM
Arie0512
Registered User
Posts: 4
Joined: Wed Jan 08, 2025 3:59 pm

Manually insert users into PHPBB3

Post by Arie0512 »

Hello,

I have installed PHPBB 3.3.14 and I want to add a user from my own register form to PHPBB.

I found out that it can be done with the following code:

Code: Select all

define('IN_PHPBB', true);
// Specify the path to you phpBB3 installation directory.
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : '/home/xxx/domains/yyy.nl/public_html/forum/';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
// The common.php file is required.

include($phpbb_root_path . 'common.' . $phpEx);

mail('[email protected]', '1', '1');

require($phpbb_root_path . 'includes/functions_user.' . $phpEx);

mail('[email protected]', '2', '2');

$username = "Test";
$user_password = "123456";
$user_email = "[email protected]";
$group_id = 2;
// $user_type = USER_NORMAL;
$user_type = 0;
$user_ip = $_SERVER['REMOTE_ADDR'];
$registration_time = time();

mail('[email protected]', '3', '3');

$user_row = array(
    'username'              => $username,
    'user_password'         => phpbb_hash($user_password),
    'user_email'            => $user_email,
    'group_id'              => $group_id,
    'user_type'             => $user_type,
    'user_ip'               => $user_ip,
    'user_regdate'          => $registration_time
);

mail('[email protected]', '4', '4');

// Register user...
$user_id = user_add($user_row, false, null);
I got no error in the log files on the webserver and also no error in the console.

I only get email 1 and 2 in my mailbox as a test where the code breaks so what is wrong with the setting of the variables?

I can't figure it out, it should be a very stupid thing .......... :roll:

Kind regards,

Arie
Last edited by thecoalman on Wed Jan 08, 2025 10:36 pm, edited 1 time in total.
Reason: Moved to Custom Coding
User avatar
wintstar
Registered User
Posts: 363
Joined: Sat Mar 07, 2009 12:39 pm
Location: Central Hessen, close to the "heart of nature", Germany

Re: Manually insert users into PHPBB3

Post by wintstar »

Arie0512
Registered User
Posts: 4
Joined: Wed Jan 08, 2025 3:59 pm

Re: Manually insert users into PHPBB3

Post by Arie0512 »

Hi,

I have seen this addon but can I add a user from my own Php login script, I thought that this addon can create a user from the ACP? Because that is not the wat I want to add a New user to the PHPBB forum.

Kind regards

Arie
User avatar
Anișor
Translator
Posts: 361
Joined: Tue Jan 08, 2013 9:36 pm
Location: Angus, Scotland 🏴󠁧󠁢󠁳󠁣󠁴󠁿

Re: Manually insert users into PHPBB3

Post by Anișor »

You might want to add the extra checks for username length, error handling, etc.
This works with the file located in the root directory. Adjust it as needed.

Code: Select all

<?php
define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
require($phpbb_root_path . 'common.' . $phpEx);
require($phpbb_root_path . 'includes/functions_user.' . $phpEx);
require($phpbb_root_path . 'includes/functions_module.' . $phpEx);

global $config;
global $phpbb_container;
global $user;

$timezone = $config['board_timezone'];

//User data
$data = array(
    'username'			=> 'UserTest1',
    'new_password'		=> '1234567',
    'password_confirm'	=> '1234567',
    'email'				=> '[email protected]',
    'group_id'          => 2,
    'lang'				=> 'en',
    'tz'				=> $timezone,
);

$user_type = USER_NORMAL;
$user_actkey = strtolower(gen_rand_string(32));
$user_inactive_reason = 0;
$user_inactive_time = 0;


// Instantiate passwords manager
/* @var $passwords_manager \phpbb\passwords\manager */
$passwords_manager = $phpbb_container->get('passwords.manager');

$user_row = array(
    'username'					=> $data['username'],
    'user_password'				=> $passwords_manager->hash($data['new_password']),
    'user_email'				=> $data['email'],
    'group_id'					=> $data['group_id'],
    'user_timezone'				=> $data['tz'],
    'user_lang'					=> $data['lang'],
    'user_type'					=> $user_type,
    'user_actkey'				=> $user_actkey,
    'user_actkey_expiration'	=> $user::get_token_expiration(),
    'user_ip'					=> $user->ip,
    'user_regdate'				=> time(),
    'user_inactive_reason'		=> $user_inactive_reason,
    'user_inactive_time'		=> $user_inactive_time,
);

$user_id = user_add($user_row);
Arie0512
Registered User
Posts: 4
Joined: Wed Jan 08, 2025 3:59 pm

Re: Manually insert users into PHPBB3

Post by Arie0512 »

This php script is working fine, tnx alot!

The only thing is that it is indeed only working from the root directory.

When I move this file to my own register directory than it fails to run without any error in the log files or console.

I have set the $phpbb_root_path to my own server path, /home/xxx/domains/demo.com/public_html/forum/ in stead of ./

Any idea how to get it working from another diredctory?
User avatar
thecoalman
Community Team Member
Community Team Member
Posts: 6518
Joined: Wed Dec 22, 2004 3:52 am
Location: Pennsylvania, U.S.A.

Re: Manually insert users into PHPBB3

Post by thecoalman »

You can usually use $_SERVER["DOCUMENT_ROOT"] which is usually /root/path/to/public_html Note there is no trailing slash.

Code: Select all

require($_SERVER["DOCUMENT_ROOT"] . '/common.' . $phpEx);
You can test what it is using:

Code: Select all

echo $_SERVER["DOCUMENT_ROOT"];
exit;
“Results! Why, man, I have gotten a lot of results! I have found several thousand things that won’t work.”

Attributed - Thomas Edison
Arie0512
Registered User
Posts: 4
Joined: Wed Jan 08, 2025 3:59 pm

Re: Manually insert users into PHPBB3

Post by Arie0512 »

Tnx for the feedback!

After some testing I found out that the problem isn't in the PHPBB script ....... :shock:

After the insert of the record in the PHPBB table the code is going to insert the form into my own table.

And there I found out where the problem was:

Code: Select all

foreach ($_POST['begeleider_transporteur'] as $key => $value) {
  // echo $key . " is at " . $value . "<br />";
  if ($value === 'begeleider') {
    $begeleider = 1;
    $begeleider_ja_nee = "Ja";
  }
  if ($value === 'transporteur') {
    $transporteur = 1;
    $transporteur_ja_nee = "Ja";
  }
}
It looks like the problem is in the foreach statement where an array of checkboxes hass to pass.

I also try to change this into this

Code: Select all

if(in_array('begeleider', $_POST['begeleider_transporteur'])){ ... }
But that didn't also get it working, I get no error at all in the console or the server log.

Finally I got a little brainwash and I do my own insert first and at last the insert into the PHPBB table and that's works ...... :lol:

I have no idea what exactly went wrong with the foreach and the in_array but I have got it working!

Return to “phpBB Custom Coding”