1

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' => '[email protected]',
  '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');
1

1 Answer 1

4

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' => '[email protected]',
  '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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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