Insert a user into phpBB from PHP

Discussion forum for MOD Writers regarding MOD Development.
fametown
Registered User
Posts: 15
Joined: Fri Jan 31, 2003 12:18 pm
Location: Asturias, Spain
Contact:

Insert a user into phpBB from PHP

Post by fametown »

This is the code that i use to insert a user from a PHP code. I have a website with his own user database and i use this code to insert the same user to the phpBB forum.

I put here this "really bad" code because help me in my particular case.

Sorry for my horrible english... i'm from spain ;)

Code: Select all

<?php 

$user_type = "0";
$group_id = "2";
$user_regdate = strtotime ("now"); // Unix Timestamp
$user_passchg =  $user_regdate; // Unix Timestamp
$user_lastmark = $user_regdate; // Unix Timestamp
$username = "Test"; // Username
$username_clean = "test"; // Username in lowercase
$user_password = md5("123456"); // The password with MD5 encryption
$user_email = "myemail@server.com"; // Email address
$user_email_hash = crc32(strtolower($user_email)) . strlen($user_email); // Email hash (CRC32) with the lenght
$user_lang = "en";
$user_timezone = "1.0";

mysql_select_db($database, $conexion); // Connect to the database (i have a include)

$query_ft = "INSERT INTO phpbb_users (user_type, group_id, user_regdate, username, username_clean, user_password, user_passchg, user_lastmark, user_email, user_email_hash, user_lang, user_timezone) values ('$user_type','$group_id','$user_regdate','$username','$username_clean','$user_password','$user_passchg','$user_lastmark','$user_email','$user_email_hash','$user_lang','$user_timezone')";	
mysql_query($query_ft, $conexion) or die(mysql_error());

$ultimo_id = mysql_insert_id($conexion); 

// Insert the user in the user group table

$query_ft = "INSERT INTO phpbb_user_group (group_id, user_id, user_pending) values ('$group_id','$ultimo_id','0')";
mysql_query($query_ft, $conexion) or die(mysql_error());

?>
Fametown.com
-----------------
Helping your dreams since 1996
User avatar
A_Jelly_Doughnut
Former Team Member
Posts: 34457
Joined: Sat Jan 18, 2003 1:26 am
Location: Where the Rivers Run
Contact:

Re: Insert a user into phpBB from PHP

Post by A_Jelly_Doughnut »

You can replace all of this code below with a call to user_add() in /includes/functions_user.php. You just need to put the user's information in an array with the database field as the key and the data as the value.

For full details on how to use this function, look at /includes/ucp/ucp_register.php :)

Code: Select all

mysql_select_db($database, $conexion); // Connect to the database (i have a include)

$query_ft = "INSERT INTO phpbb_users (user_type, group_id, user_regdate, username, username_clean, user_password, user_passchg, user_lastmark, user_email, user_email_hash, user_lang, user_timezone) values ('$user_type','$group_id','$user_regdate','$username','$username_clean','$user_password','$user_passchg','$user_lastmark','$user_email','$user_email_hash','$user_lang','$user_timezone')";   
mysql_query($query_ft, $conexion) or die(mysql_error());

$ultimo_id = mysql_insert_id($conexion);

// Insert the user in the user group table

$query_ft = "INSERT INTO phpbb_user_group (group_id, user_id, user_pending) values ('$group_id','$ultimo_id','0')";
mysql_query($query_ft, $conexion) or die(mysql_error());
A Donut's Blog
"Bach's Prelude (Cello Suite No. 1) is driving Indiana country roads in Autumn" - Ann Kish
Caruanas
Registered User
Posts: 10
Joined: Tue Aug 07, 2007 9:25 pm

Re: Insert a user into phpBB from PHP

Post by Caruanas »

How would such a call be done in php?

I have tried using include(proper_path_details_etc/includes/functions_user.php) and then calling user_add() by providing an array like the one in ucp_register.php (making sure the values are correct) however the user is not being added. I believe this is due to the fact that user_add() has the following line of code:

global $db, $user, $auth, $config, $phpbb_root_path, $phpEx;

which I believe will not be recognised one i included the functions_user.php in my own file.

Hence my conclusion that I am calling the user_add() function in a "bad way". Any suggestions?
User avatar
D¡cky
Former Team Member
Posts: 11812
Joined: Tue Jan 25, 2005 8:38 pm
Location: New Hampshire, USA
Name: Richard Foote
Contact:

Re: Insert a user into phpBB from PHP

Post by D¡cky »

Move from Convertors to MOD Writer's Discussion
Have you hugged someone today?
User avatar
hotmit
Registered User
Posts: 16
Joined: Sat Oct 13, 2007 3:40 pm

Re: Insert a user into phpBB from PHP

Post by hotmit »

Caruanas wrote:How would such a call be done in php?

I have tried using include(proper_path_details_etc/includes/functions_user.php) and then calling user_add() by providing an array like the one in ucp_register.php (making sure the values are correct) however the user is not being added. I believe this is due to the fact that user_add() has the following line of code:

global $db, $user, $auth, $config, $phpbb_root_path, $phpEx;

which I believe will not be recognised one i included the functions_user.php in my own file.

Hence my conclusion that I am calling the user_add() function in a "bad way". Any suggestions?
try to put this code before you call the user_add() method, cuz it will initiallize all the variable you need (like $db, $user etc.)

Code: Select all

define('IN_PHPBB', true);

// use this, if it does work then specifiy your own path
// something like this $phpbb_root_path = './public/forum/';
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';     

$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);

// may be you don't need this line, but include it anyway
include($phpbb_root_path . 'includes/functions_display.' . $phpEx);

// Start session management
$user->session_begin();    
$auth->acl($user->data);
User avatar
joeroberts
Registered User
Posts: 272
Joined: Fri Jun 22, 2007 7:19 pm
Contact:

Re: Insert a user into phpBB from PHP

Post by joeroberts »

Here is my php function to add users to phpBB3 with my integration

Code: Select all

function forumadd($username){
global $db, $forumpx, $db_prefix, $forumbase, $phpEx, $config;
define('FORUM_ADD',TRUE);
include_once('./'.$forumbase . '/common.'.$phpEx);
include_once('./'.$forumbase . '/includes/functions_user.' . $phpEx);
global $config;
define('IN_PHPBB',TRUE);
define('IN_PORTAL',TRUE);
$phpbb_root_path = './phpBB3/';

		$coppa			= (isset($_REQUEST['coppa'])) ? ((!empty($_REQUEST['coppa'])) ? 1 : 0) : false;

	$group_name = ($coppa) ? 'REGISTERED_COPPA' : 'REGISTERED';
        $sql = 'SELECT group_id
        FROM ' . GROUPS_TABLE . "
        WHERE group_name = '" . $db->sql_escape($group_name) . "'
            AND group_type = " . GROUP_SPECIAL;
$result = $db->sql_query($sql)or print(mysql_error());
$row = $db->sql_fetchrow($result);
$group_id = $row['group_id'];
$res = "SELECT id,username,password,email FROM ".$db_prefix."_users WHERE username ='".escape($username)."';";
$result = $db->sql_query($res)or print(mysql_error());
$num = mysql_num_rows($result);
$user_actkey = md5(rand(0, 100) . time());
$user_actkey = substr($user_actkey, 0, rand(6, 10));
$timezone = '-6';
$language = 'en';
$user_type = USER_NORMAL;
$is_dst = date('I');
		$arr = $db->sql_fetchrow($result);
				$user_row = array(
					'username'				=> $arr['username'],
					'user_password'			=> $arr['password'],
					'user_email'			=> $arr['email'],
					'group_id'				=> (int) $group_id,
					'user_timezone'			=> (float) $timezone,
					'user_dst'				=> $is_dst,
					'user_lang'				=> $language,
					'user_type'				=> $user_type,
					'user_actkey'			=> $user_actkey,
					'user_regdate'			=> time(),
				);

				// Register user...
				 user_add($user_row);
}

and I had to add a mod to common.php

Code: Select all

if (defined('FORUM_ADD'))$phpbb_root_path= './phpBB3/';
right before

Code: Select all

if (!file_exists($phpbb_root_path . 'config.' . $phpEx))
then on my sign up confirm I add (forumadd($username))
this code dose a complete user add with the use of phpBB3 function user_add()
User avatar
paulfromabmatch
Registered User
Posts: 94
Joined: Fri Sep 21, 2007 1:08 am
Location: Hartford,Connecticut,USA

Re: Insert a user into phpBB from PHP

Post by paulfromabmatch »

I dont know if this will help but this is how I created new members in my php site..

I modified it from a phpBB2 version is now working for rc7...

it works for me but have not test thorougly just to give you an idea...

Code: Select all

/ phpbb: register
			///////////////////////////////////////////////////////////////////////
			$r = f(q("SELECT MAX(user_id) AS total FROM ".USERS_TABLE));
			
			$phpbb_userid = $r['total']+1;	// user id they will have in phpBB
		
			$sql = "INSERT INTO " . USERS_TABLE . "
												(user_id,		
		
username,
username_clean,			
user_password,			
user_type,		
group_id,
user_permissions,	
user_regdate,
user_passchg,
user_pass_convert,
user_email,
user_lastvisit,	
user_lang,	
user_dateformat,
user_style,
user_rank,
user_full_folder,
user_topic_sortby_type,
user_topic_sortby_dir,
user_post_sortby_type,
user_post_sortby_dir,
user_notify_pm,
user_allow_pm,
user_allow_viewonline,
user_allow_massemail,
user_options,
user_avatar,
user_avatar_type,
user_avatar_width,
user_avatar_height,	
user_sig_bbcode_uid				)
							VALUES
								($phpbb_userid,
								'" . str_replace("\'", "''", $login) . "', 
								'" . str_replace("\'", "''", $login) . "', 
								'" . str_replace("\'", "''", md5($pswd)) . "', 
								0,
								16805,
								'',
								" . time() . ",
								0,
								1,
								'" . str_replace("\'", "''", $email) . "' ,
								" . time() . ",
								'en',
								'D M d, Y g:i a',
								1,
								0,
								-3,
								't',
								'd',
								't',
								'a',
								1,
								1,
								1,
								1,
								1919,
								'" . str_replace("\'", "''", $avl) . "',
								2,
								11,
								25,
								'93712'
								)";
			q($sql);
			//update config filr
			$sql="UPDATE phpbb3_config SET config_value ='$login'  WHERE config_name ='newest_username' ";
			q($sql);
			$sql="UPDATE phpbb3_config SET config_value ='$phpbb_userid'  WHERE config_name ='num_users' ";
			q($sql);
			$sql="UPDATE phpbb3_config SET config_value ='$phpbb_userid'  WHERE config_name ='newest_user_id' ";
			q($sql);
	        // Add group for user
			$sql = "INSERT INTO phpbb3_user_group
							(group_id,user_id,group_leader,user_pending )
							VALUES
							(16805,$phpbb_userid,0,0)";
			q($sql);
			

			// Get group_id for newly created group (above)
This is just an idea... I do not back any of this code...

may have bugs
User avatar
igorw
Former Team Member
Posts: 8024
Joined: Fri Dec 16, 2005 12:23 pm
Location: {postrow.POSTER_FROM}
Name: Igor Wiedler

Re: Insert a user into phpBB from PHP

Post by igorw »

paulfromabmatch,

i would suggest using user_add() instead, why create all those queries if you can just use what phpbb provides you with? ;)
Igor Wiedler | area51 | GitHub | trashbin | Formerly known as evil less than three
User avatar
paulfromabmatch
Registered User
Posts: 94
Joined: Fri Sep 21, 2007 1:08 am
Location: Hartford,Connecticut,USA

Re: Insert a user into phpBB from PHP

Post by paulfromabmatch »

well... the function is not available from the page that does this... so I used a modification of a previous mod someone else wrote... I am really trying to integrate the two sides of my site...

the original mod was written by the people who wrote the script that runs my site... I have found alot of problems with their code... so I dunno

maybe I will keep it to myself next time :)
User avatar
igorw
Former Team Member
Posts: 8024
Joined: Fri Dec 16, 2005 12:23 pm
Location: {postrow.POSTER_FROM}
Name: Igor Wiedler

Re: Insert a user into phpBB from PHP

Post by igorw »

You could integrate that page into phpBB...
Igor Wiedler | area51 | GitHub | trashbin | Formerly known as evil less than three
User avatar
joeroberts
Registered User
Posts: 272
Joined: Fri Jun 22, 2007 7:19 pm
Contact:

Re: Insert a user into phpBB from PHP

Post by joeroberts »

eviL<3 wrote:paulfromabmatch,

i would suggest using user_add() instead, why create all those queries if you can just use what phpbb provides you with? ;)
mine only uses one more query then phpBB and that is to get the users info from one user row to pass on to phpBB.
I have it set to work on the users confirmation email so that it don't add a user that is fake or just fooling around.
This hole thing works with My forum integration for phpMyBitTorrent to phpBB3 and works 100% on 3 live sites.
adamsargant
Registered User
Posts: 9
Joined: Tue Nov 21, 2006 11:33 pm

Re: Insert a user into phpBB from PHP

Post by adamsargant »

I've succesfully written a piece of code from what has been written here that will use user_add() to enter people into the phpbb3 database... what I cannot work out is how to hash the password so that it works on login... any ideas? Basically, I want my phpbb to be a smaller part of a larger log-in area for my users, so I'm looking to use the phpbb user table as my single point of reference for registration and log-in, but I don't want my users to have to sign in from the forum... the subscribe and sign in from my log-in page which then gives them access to the forum.

Adam
adamsargant
Registered User
Posts: 9
Joined: Tue Nov 21, 2006 11:33 pm

Re: Insert a user into phpBB from PHP

Post by adamsargant »

Chuffin' typical... I make my forehead bleed working on this, post a request, and the answer comes to me minutes later

if it's useful to anyone

Code: Select all

define('IN_PHPBB', true);
$phpbb_root_path = 'pathtoroot/';

$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.php');
include($phpbb_root_path . 'includes/functions_user.php');
include($phpbb_root_path . 'includes/ucp/ucp_register.php');

$password='pword';
$username='uname';
$username_clean=strtolower($username);
$user_email='email@email.net';

    $sql_ary = array(
        'username'          => $username,
        'username_clean'    => $username_clean,
        'user_password'     => phpbb_hash($password),
        'user_pass_convert' => 0,
        'user_email'        => $user_email,
        'user_email_hash'   => crc32(strtolower($user_email)) . strlen($user_email),
        'group_id'          => 2,
        'user_type'         => 0,
    );
user_add($sql_ary);
schuel
Registered User
Posts: 10
Joined: Thu Sep 27, 2007 3:23 am

Re: Insert a user into phpBB from PHP

Post by schuel »

adamsargant wrote:Chuffin' typical... I make my forehead bleed working on this, post a request, and the answer comes to me minutes later

if it's useful to anyone

Code: Select all

define('IN_PHPBB', true);
$phpbb_root_path = 'pathtoroot/';

$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.php');
include($phpbb_root_path . 'includes/functions_user.php');
include($phpbb_root_path . 'includes/ucp/ucp_register.php');

$password='pword';
$username='uname';
$username_clean=strtolower($username);
$user_email='email@email.net';

    $sql_ary = array(
        'username'          => $username,
        'username_clean'    => $username_clean,
        'user_password'     => phpbb_hash($password),
        'user_pass_convert' => 0,
        'user_email'        => $user_email,
        'user_email_hash'   => crc32(strtolower($user_email)) . strlen($user_email),
        'group_id'          => 2,
        'user_type'         => 0,
    );
user_add($sql_ary);
Thank you so much!!! I've been searching for an answer on how to do this for so long. The code I had never worked and no one seemed to be able to tell me why. However I tried your code and it worked first time! Much appreciated!
adamsargant
Registered User
Posts: 9
Joined: Tue Nov 21, 2006 11:33 pm

Re: Insert a user into phpBB from PHP

Post by adamsargant »

Cool, glad it is of use... now I have a remote log-in script, I have a subscriber script, now all I need to be able to do is check that someone is logged in on my non-phpbb pages.

I've been trying to do everything I want with external code rather than modifications, because mods are a pain to keep track of and I don't want to run the risk of breaking something with a future mod if I have to use one.

Now I look back at the code, I'm not entirely sure that includes/functions_user.php is necessary, it may already be pulled in by common.php, but I will have to check later.
Locked

Return to “[3.0.x] MOD Writers Discussion”