Community Documentation

Auto login from customised e-mail

Last updated July 3, 2008. Created by anbuindia on May 15, 2008.
Edited by add1sun. Log in to edit this page.

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');
}
?>

Comments

Help with setup

Hello, can you point out a how-to page/tutorial on this or give some directions? We have a content type that we need to be only accessible from authenticated users through an email like you are saying here. Thanks in advance!

About this page

Drupal version
Drupal 5.x

Reference

Drupal’s online documentation is © 2000-2012 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.