Bulk Add Users

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

Re: Bulk Add Users

Post by BrennanU »

I dont mean to be rude, but I dont know much about php or phpbb. Could someone give me the exact code that I need to use to retrieve users from the csv once it is figured out?
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 »

BrennanU wrote:I dont mean to be rude, but I dont know much about php or phpbb. Could someone give me the exact code that I need to use to retrieve users from the csv once it is figured out?
Ok I have rewritten the file from scratch and this time tested it.
  1. You will need a csv with these fields:

    Code: Select all

    "User 01","pwd01","[email protected]",2,"en",0
    "User 02","pwd02","[email protected]",2,"en",0
    "User 03","pwd03","[email protected]",2,"en",0
    Where the fields are in order:

    Code: Select all

    $users_data[] = array(
    	0 => 'username',
    	1 => 'password',
    	2 => 'email',
    	3 => 'group_id',
    	4 => 'lang',
    	5 => 'type',
    );
  2. You should call this file users_to_add.csv and put it in your forum root. You can name it differently, but then you will need to adjust name and path in the PHP file I'm going to post.
  3. Create a PHP files called users_add.php and put in this code:

    Code: Select all

    <?php
    /**
    *
    * @package phpBB3
    * @version $Id: users_add.php,v 1.0.0 2008/07/29 00:00:00 Mighty Gorgon Exp $
    * @copyright (c) 2008 Mighty Gorgon
    * @license http://opensource.org/licenses/gpl-license.php GNU Public License
    *
    */
    
    /**
    */
    
    /**
    * @ignore
    */
    
    @set_time_limit(0);
    //@ignore_user_abort(true);
    @ini_set('memory_limit', '64M');
    
    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();
    
    $users_start = request_var('us', 0);
    $users_step = request_var('up', 50);
    
    $users_data = array();
    
    /*
    $users_data[] = array(
    	'username' => 'username',
    	'user_password' => 'password',
    	'user_email' => 'email',
    	'group_id' => 2,
    	'user_lang' => 'en',
    	'user_type' => 0,
    	'user_regdate' => '',
    );
    
    $users_data[] = array(
    	0 => 'username',
    	1 => 'password',
    	2 => 'email',
    	3 => 'group_id',
    	4 => 'lang',
    	5 => 'type',
    	6 => 'regdate',
    );
    
    CSV Example:
    "User 01","pwd01","[email protected]",2,"en",0
    "User 02","pwd02","[email protected]",2,"en",0
    "User 03","pwd03","[email protected]",2,"en",0
    */
    
    $users_counter = 0;
    $handle = fopen('users_to_add.csv', 'r');
    while (($data = fgetcsv($handle, 0, ',')) !== false)
    {
    	$num = count($data);
    	for ($c = 0; $c < $num; $c++)
    	{
    		$users_data[$users_counter][$c] = $data[$c];
    	}
    	$users_counter++;
    }
    fclose($handle);
    
    //include($phpbb_root_path . 'users_to_add.' . $phpEx);
    
    $users_list = '';
    $total_users = count($users_data);
    $users_this_step = min($users_start + $users_step, $total_users);
    $users_this_step = ($users_this_step == 0) ? $total_users : $users_this_step;
    $new_start = $users_start;
    
    $users_fields_name = array('username', 'user_password', 'user_email', 'group_id', 'user_lang', 'user_type', 'user_regdate');
    for ($i = $users_start; $i < $users_this_step; $i++)
    {
    	$users_fields_values = array($users_data[$i][0], phpbb_hash($users_data[$i][1]), $users_data[$i][2], '2', 'en', '0', time());
    	for ($j = 0; $j < count($users_fields_name); $j++)
    	{
    		$users_data[$i][$j] = (empty($users_data[$i][$j])) ? $users_fields_values[$j] : $users_data[$i][$j];
    		$users_data[$i][$users_fields_name[$j]] = $users_fields_values[$j];
    		unset($users_data[$i][$j]);
    	}
    	$user_id = user_add($users_data[$i]);
    	if ($user_id !== false)
    	{
    		$users_list .= (($users_list == '') ? '' : ', ') . $users_data[$i]['username'];
    	}
    	$new_start++;
    }
    
    $message_text = 'The following users have been added to the DB:<br /><br />' . $users_list;
    
    if ($new_start >= $total_users)
    {
    	$message = 'All the users have been imported correctly!<br /><br />' . $message_text;
    	$template->assign_vars(array(
    		'MESSAGE_TITLE' => 'ADDING USERS',
    		'MESSAGE_TEXT' => $message
    		)
    	);
    	page_header('ADDING USERS');
    	$template->set_filenames(array('body' => 'message_body.html'));
    	page_footer();
    }
    else
    {
    	$meta_url = append_sid("{$phpbb_root_path}users_add.$phpEx", "us=$new_start&up=$users_step");
    	meta_refresh(3, $meta_url);
    	$message = 'The process is not finished yet, this page will redirect automatically in few seconds, please wait...<br /><br />' . $message_text;
    	trigger_error($message);
    	exit;
    }
    
    ?>
    Again if you want to name it differently then you have to adjust all names and path in it.
  4. Launch the file and it will process 50 users per step... you may specify a different amount of users per step by appending this var up=XX where XX is the number you like.
  5. The script should process all datas in CSV and import all users... if you are importing more than 50 users then you need to wait the script to process them from step to step... this should happen automatically, unless errors occurs...
Let me know if this time everything is working fine... I have tested it on my board and it worked.
BrennanU
Registered User
Posts: 11
Joined: Sun Jul 20, 2008 12:53 pm

Re: Bulk Add Users

Post by BrennanU »

All of the users add for me, the only thing is that whatever group id I input, it doesnt add the user to the group.
harley1979fxe
Registered User
Posts: 8
Joined: Fri Jul 25, 2008 12:12 pm

Re: Bulk Add Users

Post by harley1979fxe »

This worked for me after I made one change.

Changed Mighty Gorgon's users_add.php

Code: Select all

while (($data = fgetcsv($handle, 0, ',')) !== false)
to

Code: Select all

while (($data = fgetcsv($handle, 200, ',')) !== false)
I have PHP 4.x.x right now and the length parameter in fgetcsv() won't work with 0.
Per http://www.php.net fgetcsv() info:
length
Must be greater than the longest line (in characters) to be found in the CSV file (allowing for trailing line-end characters). It became optional in PHP 5. Omitting this parameter (or setting it to 0 in PHP 5.0.4 and later) the maximum line length is not limited, which is slightly slower.
I set the length parameter to 200 (which is longer than my longest entry), and it worked fine.

My users all are in the pre-defined group "Registered Users". I don't have any user defined groups.

Thank you very much! This helps a great deal!
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 »

BrennanU wrote:All of the users add for me, the only thing is that whatever group id I input, it doesnt add the user to the group.
Mmm... that is strange... are you sure that the group exists and can accept new users?

Do you have a link to check?
harley1979fxe wrote:Thank you very much! This helps a great deal!
You are welcome... glad you solved. :D
harley1979fxe
Registered User
Posts: 8
Joined: Fri Jul 25, 2008 12:12 pm

Re: Bulk Add Users

Post by harley1979fxe »

One other thing to note: When I export (save as) from Excel to .csv, " characters show up as """.
I found that there is no need for the double quote around the text fields as it is interpreted correctly when called in with ftpgetcsv. Thanks again!
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:One other thing to note: When I export (save as) from Excel to .csv, " characters show up as """.
That is because Excel escapes double quotes with double double quotes... you need to specify settings differently while exporting.
harley1979fxe wrote:I found that there is no need for the double quote around the text fields as it is interpreted correctly when called in with ftpgetcsv. Thanks again!
I guess if you have commas in fields you will have problems... it is always a good thing escaping special chars and strings.
BrennanU
Registered User
Posts: 11
Joined: Sun Jul 20, 2008 12:53 pm

Re: Bulk Add Users

Post by BrennanU »

I am sure the groups exist. It is just a test board for now at http://www.brennanu.com/phpbb3. I will PM you the admin login.

*edit: It looks like the script asks for the group id, but later it just sets it as 2,as in the group id line here

Code: Select all

$users_data[] = array(
   'username' => 'username',
   'user_password' => 'password',
   'user_email' => 'email',
   'group_id' => 2,
   'user_lang' => 'en',
   'user_type' => 0,
   'user_regdate' => '',
);
harley1979fxe
Registered User
Posts: 8
Joined: Fri Jul 25, 2008 12:12 pm

Re: Bulk Add Users

Post by harley1979fxe »

In the code, I think you could just change this part...where it is hard coded to 2

Code: Select all

$users_fields_values = array($users_data[$i][0], phpbb_hash($users_data[$i][1]), $users_data[$i][2], '2', 'en', '0', time());
and use the data from your csv like this:

Code: Select all

$users_fields_values = array($users_data[$i][0], phpbb_hash($users_data[$i][1]), $users_data[$i][2], $users_data[$i][3], 'en', '0', time());
I didn't test this, but it makes sense to me that rather than using the constant value of 2, calling in $users_data[$i][3] would give you the proper values pulled from your csv file.
BrennanU
Registered User
Posts: 11
Joined: Sun Jul 20, 2008 12:53 pm

Re: Bulk Add Users

Post by BrennanU »

Thanks to harley1979fxe and Mighty Gorgon. I have sucessfully added three test users to the board and to their groups.
hammi99
Registered User
Posts: 5
Joined: Sun Sep 21, 2008 9:37 pm

Re: Bulk Add Users

Post by hammi99 »

Hi guys,

Wonderful work you have all created here. I must plead ignorance! I have followed all the steps exactly as laid out in this forum but could not get the add users to work properly.

I created a CSV file exactly as specified. Did it both in Excel exported as CSV as well as in txt file, of course, naming it correctly. I uploaded the csv file to forum's root directory and created the php script as instructed, also in same root directory.

The only difference in the PHP file was that I changed the fgetcsv to 200 also as instructed. I followed each and every syntax as well. I did not make the change from 50 users per run nor hardcoding the '2' to the group id as suggested in these forum postings.

But after running the php script it didn't work. My browser just returned a blank screen with no text. I have just one user in my users_to_add.csv file and here's the contents:
"HamTest","hammi","[email protected]",2,"en",0

the php file is at:
http://wallst-training.com/forum/users_add.php

going to my forum, you can see that the user is not added after running the users_add.php script:
http://www.wallst-training.com/forum/memberlist.php

I do not have any groups setup, only the default registered users group. I am running phpBB 2.0.18 and PHP that comes with yahoo Web Hosting but that shouldn't matter since phpBB is working fine... (or does it matter). Noting that this is phpBB 3.0 bulletins, is there something comparable for phpBB 2.0.x.

Please help!

Thanks much to all and keep up the great work!
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 »

Try to change this:

FIND

Code: Select all

while (($data = fgetcsv($handle, 0, ',')) !== false)
REPLACE WITH

Code: Select all

while (($data = fgetcsv($handle, 100000, ',')) !== false)
Let me know.
hammi99
Registered User
Posts: 5
Joined: Sun Sep 21, 2008 9:37 pm

Re: Bulk Add Users

Post by hammi99 »

unfortunately that didn't seem to work. i still get the same thing - nothing. blank file. does it matter which version of phpbb i'm running?
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 »

It should work with phpBB 3.0.2... I didn't test the script with older version.

Maybe your CSV is not properly formatted... can you paste here just one line (replace username and password with DEMO if you wish) so we can check that it is properly created?
hammi99
Registered User
Posts: 5
Joined: Sun Sep 21, 2008 9:37 pm

Re: Bulk Add Users

Post by hammi99 »

here is the csv
HamTest hammi [email protected] 2 en 0

i copied from excel (yes saved as csv file not xls)

when opening from notepad, this is how it looks:
"HamTest","hammi","[email protected]",2,"en",0

the quotes are included exactly as you see in the .csv file.


thanks.
Locked

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