Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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
Comment #1
Tech_Musings CreditAttribution: Tech_Musings commentedHi,
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
Comment #2
W.M. CreditAttribution: W.M. commentedBut 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?
Comment #3
prsnjtbarman CreditAttribution: prsnjtbarman as a volunteer and commented$uid = user_authenticate($username, $password);
$user = user_load($uid);
$form_state = array();
$form_state['uid'] = $uid;
user_login_submit(array(), $form_state);
Comment #4
testtest123432 CreditAttribution: testtest123432 as a volunteer commentedif(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']));
}