Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

according to this I wrote below code to create temp user and login , , user create but it not logged in. where is problem?

   $user_info = array(
  'name' => 'temp-payment-'.rand(1,999),
  'pass' => user_password(),
  'mail' => 'no@no.com',
  'init' => 'temp-user-'.rand(1,999),
  'status' => 1,
  'roles' => array(DRUPAL_AUTHENTICATED_RID => TRUE),
                    'access' => time(),
                    );
  $user = user_save('',$user_info);



  if($user) {
   $GLOBALS['user'] = $user;
    drupal_save_session(TRUE);
    ym($user);
  }
  drupal_goto('/user');
share|improve this question
1  
Also relevant: How do I programmatically log in a user? –  Felix Eve Mar 6 at 15:58

1 Answer 1

up vote 2 down vote accepted

Solution is

 $form_state['uid'] = $user->uid;
 user_login_submit(array(), $form_state);

and final code :

$user_info = array(
  'name' => 'temp-payment-'.rand(1,999),
  'pass' => user_password(),
  'mail' => 'no@no.com',
  'init' => 'temp-user-'.rand(1,999),
  'status' => 1,
  'roles' => array(DRUPAL_AUTHENTICATED_RID => TRUE),
                    'access' => time(),
                    );
  $user = user_save('',$user_info);
//drupal_save_session(FALSE);
//$loggedin = user_authenticate($user->name, $user->pass);
//ym($loggedin);
$form_state['uid'] = $user->uid;
 user_login_submit(array(), $form_state);
//drupal_save_session(FALSE);
$GLOBALS['user'] = $user;
drupal_save_session(TRUE);
//drupal_goto('/user');

hope be useful for another users,.

This code generate and login to it programmatically

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.