User add
From phpBB Development Wiki
user_add –– Adds a new user to phpBB3
Contents |
Description
int user_add ( Array $user_row [, Array $cp_data ] )
The user_add function is responsible for adding new users to the phpBB3 system. Because of the complexities of the database, users should only be added to phpBB3 using this function.
Parameters
user_row
An array containing the following keys (and the appropriate values): username, group_id (the group to place the user in), user_email and the user_type(usually 0). Additional entries not overridden by defaults will be forwarded.
cp_data
An array containing the custom profile field data for the user being added. See custom_profile::submit_cp_field
Return Values
Returns the new user’s user_id int, false if username, group_id, user_email, or user_type are missing from the user_row, or if username_clean was not able to be generated from the username.
Examples
Example #1 Adding a new user
This example will create a new user that is active, the user does not have to be activated to login.
// In this example, the $data array should contain the validated input fields for
// username, password, email, timezone (tz) and the user’s chosen language (lang).
$user_row = array(
'username' => $data['username'],
'user_password' => phpbb_hash($data['password']),
'user_email' => $data['email'],
'group_id' => 2, // by default, the REGISTERED user group is id 2
'user_timezone' => (float) $data['tz'],
'user_lang' => $data['lang'],
'user_type' => USER_NORMAL,
'user_ip' => $user->ip,
'user_regdate' => time(),
);
// Register user...
$user_id = user_add($user_row);
Example #2 Adding a new user with Custom Profile Fields (CPF)
This example will create a new inactive user along with the custom profile data from a form.
$cp = new custom_profile();
$error = $cp_data = $cp_error = array();
// validate and register the custom profile fields
$cp->submit_cp_field('register', $user->get_iso_lang_id(), $cp_data, $error);
// create an inactive user key to send to them...
$user_actkey = gen_rand_string(10);
$key_len = 54 - (strlen($server_url));
$key_len = ($key_len < 6) ? 6 : $key_len;
$user_actkey = substr($user_actkey, 0, $key_len);
// set the user to inactive and the reason to "newly registered"
$user_type = USER_INACTIVE;
$user_inactive_reason = INACTIVE_REGISTER;
$user_inactive_time = time();
// setup the user array for the new user
$user_row = array(
'username' => $data['username'],
'user_password' => phpbb_hash($data['password']),
'user_email' => $data['email'],
'group_id' => (int) $group_id,
'user_timezone' => (float) $data['tz'],
'user_dst' => $is_dst,
'user_lang' => $data['lang'],
'user_type' => $user_type,
'user_actkey' => $user_actkey,
'user_ip' => $user->ip,
'user_regdate' => time(),
'user_inactive_reason' => $user_inactive_reason,
'user_inactive_time' => $user_inactive_time,
);
// Register user...
$user_id = user_add($user_row, $cp_data);
// If creating the user failed, display an error
if ($user_id === false)
{
trigger_error('NO_USER', E_USER_ERROR);
}
$template->assign_vars(array(
// If there were any errors, display them, one on each newline.
'ERROR' => (sizeof($error)) ? implode('<br />', $error) : '',
));
See Also

