Bulk Add Users

Discussion forum for MOD Writers regarding MOD Development.
BrennanU
Registered User
Posts: 11
Joined: Sun Jul 20, 2008 12:53 pm

Bulk Add Users

Post by BrennanU »

Hi. Sorry if this has been posted before, but none of the topics I found have a good answer. I am managing a phpBB3 board for four high school classes, with about 120 users. Last year, I had to type each user into the registration fourm manually, which took all day. This year, I would like to type the info into a spread sheet and import it. The fields I would need to import are Username, Password, Email, and possibly a group. I have read, about the user_add() function, but I dont know much about coding. Could someone offer any help?
BrennanU
Registered User
Posts: 11
Joined: Sun Jul 20, 2008 12:53 pm

Re: Bulk Add Users

Post by BrennanU »

Anyone have a suggestion?
User avatar
ChrisRLG
Former Team Member
Posts: 3420
Joined: Wed Nov 24, 2004 3:18 pm
Location: Essex, UK
Contact:

Re: Bulk Add Users

Post by ChrisRLG »

Moving to the MOD writers forum - you might find someone with the knowledge in that room.
phpBB: The All Important Rules - Bertie Bear 3.0 - No support via PM system - use the forums please.
phpBB v2: Retirement (1/1/2009) : phpBB v3: Read Me Topic - Custom BBCodes - Support Template
Matthew 7:7"Ask and it will be given to you; seek and you will find; knock and a door will be opened to you."
My Links: MS MVP (Consumer Security) - Malware Removal:University - Own Forum: Custom BBCode testing
BrennanU
Registered User
Posts: 11
Joined: Sun Jul 20, 2008 12:53 pm

Re: Bulk Add Users

Post by BrennanU »

bump
User avatar
Mighty Gorgon
Registered User
Posts: 616
Joined: Thu May 23, 2002 2:56 pm
Location: Italy
Name: Luca Libralato
Contact:

Re: Bulk Add Users

Post by Mighty Gorgon »

Hi,
you would need to specify in your spreadsheet the following fields (or more if you need):

Code: Select all

				'username'				=> 'username'
					'user_password'			=> 'password'
					'user_email'			=> 'email'
					'group_id'				=> 2
					'user_lang'				=> 'en'
					'user_type'				=> 0
					'user_regdate'			=> ''
Then you need to create a php file (users_to_add.php for example) to be imported where each rows is like this:

Code: Select all

				$users_rows[] = array(
					'username'				=> 'username',
					'user_password'			=> 'password',
					'user_email'			=> 'email',
					'group_id'				=> 2,
					'user_lang'				=> 'en',
					'user_type'				=> 0,
					'user_regdate'			=> '',
				);
Finally you need to create a php file being able to use phpBB sessions and functions and make it process all users data to be imported:

Code: Select all

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

// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup();

include($phpbb_root_path . 'users_to_add.php');

$users_list = '';
for ($i = 0; $i < count($users); $i++)
{
    $users[$i]['user_regdate'] = time();
    $users[$i]['password'] = phpbb_hash($users[$i]['password']);
    $user_id = user_add($users[$i]);
    if ($user_id !== false)
    {
        $users_list .= (($users_list == '') ? '' : ', ') . $users[$i]['username'];
    }
}

$template->assign_vars(array(
    'MESSAGE_TITLE' => 'ADDING USERS',
    'MESSAGE_TEXT' => $users_list . '<br /><br />Have been added!'
    )
);

page_header('ADDING USERS');

$template->set_filenames(array('body' => 'message_body.html'));

page_footer();

?>
This should make the trick.
Last edited by Mighty Gorgon on Mon Jul 28, 2008 9:15 am, edited 1 time in total.
BrennanU
Registered User
Posts: 11
Joined: Sun Jul 20, 2008 12:53 pm

Re: Bulk Add Users

Post by BrennanU »

What do I do with the spreadsheet? I have it in excel now.
harley1979fxe
Registered User
Posts: 8
Joined: Fri Jul 25, 2008 12:12 pm

Re: Bulk Add Users

Post by harley1979fxe »

I am also trying to add a group of users all at once rather then having to manually enter each of them.
I have the member data in a spreadsheet and export as CSV.
Fields are username, user_password, user_email, group_id, user_lang, user_type, user_regdate.
I created users_to_add.php to read the rows to an array as follows:

Code: Select all

<?php
$file_handle = fopen("forum_users_test.csv", "rb");
while (!feof($file_handle) ) {
$line_of_text = fgets($file_handle);
$parts = explode(',', $line_of_text);
$users = array(
 	'username'            => $parts[0],
               'user_password'         => $parts[1],
               'user_email'         => $parts[2],
               'group_id'            => 2,
               'user_lang'            => 'en',
               'user_type'            => 0,
               'user_regdate'         => '',
            );
fclose($file_handle);
?> 
I echo the contents of $users['username'] and so forth....just to make sure the data is correct.
I then created addem.php to add the users to phpBB.

Code: Select all

<?php
define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);
// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup();
//echo $phpbb_root_path;
include($phpbb_root_path . 'users_to_add.php');
$users_list = '';
for ($i = 0; $i < count($users); $i++)
{
    $users[$i]['user_regdate'] = time();
    $users[$i]['password'] = phpbb_hash($users[$i]['password']);
    $user_id = user_add($users[$i]);
    if ($user_id !== false)
    {
        $users_list .= (($users_list == '') ? '' : ', ') . $users[$i]['username'];
    }
}
$template->assign_vars(array(
    'MESSAGE_TITLE' => 'ADDING USERS',
    'MESSAGE_TEXT' => $users_list . '<br /><br />Have been added!'
    )
);
page_header('ADDING USERS');
$template->set_filenames(array('body' => 'message_body.html'));
page_footer();
?>
I get error "Fatal error: Call to undefined function: user_add() in G:\Inetpub\vhosts\sitename.com\httpdocs\members\get-connected\forum\addem.php on line 20"
The files forum_users.php, users_to_add.php, and addem.php are in the root of phpBB directory (renamed forum).

Any ideas what the problem might be? Is the session/php setup in this correct?

In users_to_add I originally used $users_rows array, but when I ran addem.php, it comes back with the response of "Have been added!" with no users added.
I am a bit confused on $users_rows array or $users array in the users_to_add as there is then $users_list as well as $users.
Which should I be using to carry through my data? Is $user part of the session/phpBB setup?
harley1979fxe
Registered User
Posts: 8
Joined: Fri Jul 25, 2008 12:12 pm

Re: Bulk Add Users

Post by harley1979fxe »

Sorry, didn't mean to intrude on your thread...but I have been trying to figure out how to add users in bulk as well.
I have a member area on a web site, and I wan to add all of the members to the forum so that they don't have to register themselves (I am just added phpBB).
BrennanU
Registered User
Posts: 11
Joined: Sun Jul 20, 2008 12:53 pm

Re: Bulk Add Users

Post by BrennanU »

How do I convert the CSV to an array as in the second script?
harley1979fxe
Registered User
Posts: 8
Joined: Fri Jul 25, 2008 12:12 pm

Re: Bulk Add Users

Post by harley1979fxe »

Here is the contents of my csv test file:

billing,curtspass,[email protected],2,en,0
harley,harleypass,[email protected],2,en,0


Here is what I used to verify that the code was pulling out the data items:
(code changed to echo and display in html file)

Code: Select all

<html><body>
<?php
$file_handle = fopen("forum_users_test.csv", "rb");
while (!feof($file_handle) ) {
$line_of_text = fgets($file_handle);
$parts = explode(',', $line_of_text);
$users_rows = array(
 				'username'            => $parts[0],
               'user_password'         => $parts[1],
               'user_email'         => $parts[2],
               'group_id'            => 2,
               'user_lang'            => 'en',
               'user_type'            => 0,
               'user_regdate'         => '',
            );	
echo $users_rows['username'] . "|" . $users_rows['user_password']  . "|" .   $users_rows['user_email'] . "<BR>";
}
fclose($file_handle);
?> 
</body></html>
Here is the resulting output to browser:
(I echo with | between items for readability)

billing|curtspass|[email protected]
harley|harleypass|[email protected]


I think my array of data is o.k., I just can't get it into the phpBB table.
User avatar
igorw
Former Team Member
Posts: 8024
Joined: Fri Dec 16, 2005 12:23 pm
Location: {postrow.POSTER_FROM}
Name: Igor Wiedler

Re: Bulk Add Users

Post by igorw »

You can use fgetcsv() function for parsing CSV files.
Igor Wiedler | area51 | GitHub | trashbin | Formerly known as evil less than three
User avatar
Mighty Gorgon
Registered User
Posts: 616
Joined: Thu May 23, 2002 2:56 pm
Location: Italy
Name: Luca Libralato
Contact:

Re: Bulk Add Users

Post by Mighty Gorgon »

harley1979fxe wrote:I get error "Fatal error: Call to undefined function: user_add() in G:\Inetpub\vhosts\sitename.com\httpdocs\members\get-connected\forum\addem.php on line 20"
The files forum_users.php, users_to_add.php, and addem.php are in the root of phpBB directory (renamed forum).

Any ideas what the problem might be? Is the session/php setup in this correct?
Did you solve this?

If you are using the file I provided, I forgot to include functions_user on top of the file.

So you should add this line just below the inclusion of common.

Code: Select all

include_once($phpbb_root_path . 'includes/functions_user.' . $phpEx);
This should solve your error.

I have also edited my post.

Let me know.
harley1979fxe
Registered User
Posts: 8
Joined: Fri Jul 25, 2008 12:12 pm

Re: Bulk Add Users

Post by harley1979fxe »

I added the include for functions_user.php as you suggested.
I now get this error. I increased php.ini max execution time to 120 from 30 and still get it.

Fatal error: Maximum execution time of 120 seconds exceeded in G:\Inetpub\vhosts\sitename.com\httpdocs\\MEMBERS\get-connected\forum\includes\functions.php on line 446
This is the code from functions.php. Line 446 is the pack statement in the do while loop in the else loop.
I have only 2 users in my test csv file.

Code: Select all

	if (PHP_VERSION >= 5)
	{
		$hash = md5($salt . $password, true);
		do
		{
			$hash = md5($hash . $password, true);
		}
		while (--$count);
	}
	else
	{
		$hash = pack('H*', md5($salt . $password));
		do
		{
			$hash = pack('H*', md5($hash . $password));
		}
		while (--$count);
	}

	$output = substr($setting, 0, 12);
	$output .= _hash_encode64($hash, 16, $itoa64);

	return $output;
}
Wow, I never thought it was be this tough just to manually/programatically add a few hundred users!
User avatar
igorw
Former Team Member
Posts: 8024
Joined: Fri Dec 16, 2005 12:23 pm
Location: {postrow.POSTER_FROM}
Name: Igor Wiedler

Re: Bulk Add Users

Post by igorw »

Try adding this to the top of the file:

Code: Select all

@set_time_limit(0);
Igor Wiedler | area51 | GitHub | trashbin | Formerly known as evil less than three
harley1979fxe
Registered User
Posts: 8
Joined: Fri Jul 25, 2008 12:12 pm

Re: Bulk Add Users

Post by harley1979fxe »

Tried @set_time_limit(0); in my addem.php and also in function.php.
When I put it in function.php, it went off to lala land and eventually I got a browser error.
I put an echo statement in addem.php to echo count($users) and commented the line with user_add.
It ran until 99.9% cpu and nearly all memory on server used up. I killed the process.
...can't do that again.
Locked

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