How can i do that? Is it possible? I have some app. that should send data about users and create them, i cannot find anything abou this, any help please?

Comments

cwgordon7’s picture

user_save

markosef’s picture

<?php
  chdir($_SERVER['DOCUMENT_ROOT']);
  include_once './includes/bootstrap.inc';
  drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
  
$imis_user = user_save('', array('name' => 'test2', 'pass' => 'cookielogin',  'mail' => 'joe@smoe.org', 'init' => 'test2', 'status' => 1, 'roles' => array(DRUPAL_AUTHENTICATED_RID)), 'account');

?>

and nothing happened, what could i be doing wrong or is code wrong?

just-work’s picture

you ever get this to work?

markosef’s picture

include_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

$data['name'] = $_POST['name'];
$data['mail'] = $_POST['mail'];
$data['pass'] = $_POST['pass'];
$data['status'] = 1;
$data['roles'] = array(
DRUPAL_AUTHENTICATED_RID => 'authenticated user',
5 => 'pretplatnik', //this is my custom user and his ID is 5
);

earnshae’s picture

require_once DRUPAL_ROOT. '/includes/password.inc';

$user->pass = user_hash_password($password);
$account = user_save($user);

Note: under D7 for the password to actually work you need to store it as a hash.

capmex’s picture

Here's a way to do it via the forms api:

<?php
// register a new user
$form_state = array();
$form_state['values']['name'] = 'robo-user';
$form_state['values']['mail'] = 'robouser@example.com';
$form_state['values']['pass'] = 'password';
$form_state['values']['op'] = t('Create new account');
drupal_execute('user_register', $form_state);
?>

Above taken from: http://api.drupal.org/api/function/drupal_execute/6

Worked for me, only a minor issue, instead of only one password value the form in drupal 6 requires 2 password values or it fails:

 <?php
// this hardcodes the password value; with this you end with user:robo-user, password:password
$form_state['values']['pass']['pass1'] = 'password';
$form_state['values']['pass']['pass2'] = 'password';
// a better way is to generate a random password and assign it to a variable
$pass = user_password(8);
$form_state['values']['pass']['pass1'] = $pass;
$form_state['values']['pass']['pass2'] = $pass;
?>
Rajan M’s picture

Just $form_state['values']['pass'] = 'password';
is given in the documentation for drupal_execute(user_register', $form_state) ;

Thanks alot!

jayakrishnanj’s picture

But Password is not storing properly.. It is storing as " d41d8cd98f00b204e9800998ecf8427e " for all kind of passwords..

Please let me know.. where is my fault.

It is importing users from another database :

Following is my code :

module_load_include('inc', 'user', 'user.pages');
db_set_active('mydb');

$pmresult = db_query("SELECT * FROM {user}");
while ($username = db_fetch_object($pmresult)) {
$pmusername= $username->username ;
$pmpassword= $username->password;
db_set_active('default');

include_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

// Get an array of roles
$roles = user_roles();
print $pmpassword ;
// Basic account information
$account = array(
'name' => $pmusername,
'pass' => $pmpassword,
'mail' => $pmusername,
'status' => 1,
/*'init' => $pmusername,*/
'roles' => array(array_search('customer', $roles) => 1),
);

// See if the user exists by calling Drupal's user_load()
$existing_user = user_load(array('name' => $account['name']));

if (!$existing_user->uid) {

$form_state['values'] = $account;
$form_state['values']['pass'] = array(
'pass1' => $account['pass'],
'pass2' => $account['pass'],
);
$form_state['values']['op'] = t('Create new account');
drupal_execute('user_register', $form_state);
}
}

meustrus’s picture

If you're referring to what actually gets stored in the database, then it's not supposed to be the plain text password. Drupal only stores a one-way hash of the password to verify log-ins, not the password itself.

Blackbird’s picture

The code that markosef posted (reply #2) worked for me. To be precise, this was my code:

<?php
	include_once './includes/bootstrap.inc';
	drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

    $userinfo = array(
      'name' => 'SOME_USERNAME',
      'init' => 'SOME_USERNAME',
      'mail' => 'SOME_USERNAME',
      'pass' => 'SOME_PASSWORD',
      'status' => 1
    );

    $account = user_save('', $userinfo);

    // Terminate if an error occured during user_save().
    if (!$account) {
      $account = "Error saving user account!";
    }
?>
joshua.stout’s picture

<?php
// register a new user
$form_state = array();
$form_state['values']['name'] = 'robo-user';
$form_state['values']['mail'] = 'robouser@example.com';
$form_state['values']['pass']['pass1'] = 'password';
$form_state['values']['pass']['pass2'] = 'password';
$form_state['values']['op'] = t('Create new account');
drupal_form_submit('user_register_form', $form_state);
?>
artofeclipse’s picture

check out this web site
http://thedrupalblog.com/programmatically-addupdate-users-using-user-save

you can create a custom form to collect the data you want and create the users on submit,
I hope this help you.

nqsang’s picture

Thanks.

larsmw’s picture

Are there a guide to do this in D7?

/Lars Nielsen

nevets’s picture

I believe the link above for D6 should still apply in D7.

nqsang’s picture

Thanks.