Hello,
I tried to login a user after a commerce order completion but with no luck.
I am new in drupal programming.
In rules i load a user variable which is a replicate of commerce-order:owner.
Then i use this in an "Execute custom PHP code" action:

global $user;
$user = user_load($user->uid);
drupal_session_regenerate();

and the user doesn't login.

But when i use this code:

global $user;
$user = user_load(183); //183 a sample uid
drupal_session_regenerate();

The user logged in regularly.

How this can work?

Comments

Tech_Musings’s picture

Hi,
You can do this by using following way.
$account = array('uid' => $account->uid);
user_login_submit(array(), $account);

as in drupal 7, user_external_login has no longer exist so it should work in your case.
Thanks,
Anil

W.M.’s picture

Issue summary: View changes

But is it safe to login programatically to a Drupal 7 site using a custom PHP file which bootstraps Drupal and gets login credentials via a POST request? Thanks.

Isn't it safer to login using the Drupal login form?

prsnjtbarman’s picture

$uid = user_authenticate($username, $password);

$user = user_load($uid);

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

testtest123432’s picture

Project: Rules » Login Activity
Version: 7.x-2.3 » 7.x-1.x-dev
Component: Module Integrations » Code

if(user_authenticate($form_state['values']['name'], $form_state['values']['password'])) {

$user_obj = user_load_by_name($form_state['values']['name']);
$form_state['uid'] = $user_obj->uid;
user_login_submit($form,$form_state);
return true;

}
else {
form_set_error('name', t('Sorry, unrecognized username or password.'));
watchdog('user', 'Login attempt by unregistered user %user.', array('%user' => $form_state['values']['name']));

}