My users were complaining that when they logged back in after being autologged-out, they were being redirected to the home page rather than the page they had intended to go to. I solved this with the following functions in my 'mcutils' module. Thought it might be useful as an added feature to the autologout module.
/**
* Implementation of hook_user().
*
* In the event that the autologout module has timed out and the
* user subsequently requests a URL, the URL is stored in the user's
* data string upon logout then restored when the user logs back in.
*/
function mcutils_user($op, &$edit, &$account, $category = NULL) {
switch ($op) {
case 'login':
if ($account->uid > 0) {
$user_data = mcu_get_user_data($account->uid);
if ($user_data['destination']) {
$_REQUEST['destination'] = $user_data['destination'];
}
}
break;
case 'logout':
if ($account->uid > 0) {
$destination = substr(drupal_get_destination(), 12);
mcu_update_user_data($destination, $account->uid);
}
break;
}
}
/**
* Retrieves user's data and returns it as an array
*/
function mcu_get_user_data($uid) {
$sql = "SELECT data
FROM {users}
WHERE uid = %d";
$user = db_fetch_object(db_query($sql, $uid));
$user_data = unserialize($user->data);
return $user_data;
}
/**
* Updates user's data with destination information
*/
function mcu_update_user_data($destination, $uid) {
$user_data = mcu_get_user_data($uid);
$user_data['destination'] = $destination;
$data = serialize($user_data);
$sql = "UPDATE {users}
SET data = '%s'
WHERE uid = %d";
$sql_args = array(
$data,
$uid,
);
db_query($sql, $sql_args);
}
Comments
Comment #1
james marks commentedOops. Found (and fixed) a bug in the previous code where, if the user deliberately logged out, 'logout' was stored as the destination and, on each subsequent attempt to log back in, the user was promptly returned to 'logout' thus blocking them from ever logging in.
Comment #2
james marks commentedThis is a better solution. (Sorry, should have thought about it for a bit before posting the last fix.)
Comment #3
jvandervort commentedMoved to the current dev head.
Comment #4
Ela commentedsubscribing
Comment #5
macandrow commentedThis is just the solution I was looking for but where does it go? Am I creating a new module or attaching this to an existing one?w
Comment #6
dmadruga commentedI'm just wondering if there's something wrong with adding a drupal_get_destination() as second argument of drupal_goto('autologout/logout');. It becomes
drupal_goto('autologout/logout',drupal_get_destination());
I don't see the reason why the destination value must be stored. Am I missing something?
Best regards,
Daniel
Comment #7
jvandervort commentedCommitted to 6.x-2.x-dev.
If you leave the redirect url blank in the admin settings, it should remember where you were headed and send you there when you type in your credentials.