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' => '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');
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' => '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

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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