Auto login from customised e-mail

Here is a function for auto login. Its useful when we want to make a user as logged in from clicking our
URL from e-mail.

You will need to add a menu callback in your module's hook_menu for the URL you wish to send out.

<?php
function alternate_user_login($encoded_uid = NULL) {
  global
$user;
 
$uid = base64_decode($encoded_uid);
 
// Check if the user is already logged in. The back button is often the culprit here.
 
if ($user->uid) {
   
drupal_goto('');
  }
 
$account = user_load(array('uid' => $uid));
 
$user = $account;
 
// And proceed with normal login, going to user page.
 
$edit = array();
 
user_module_invoke('login', $edit, $user);
 
sess_regenerate();
 
drupal_goto('user/'.$user->uid.'/edit');
}
?>

 
 

Drupal is a registered trademark of Dries Buytaert.