You are here

function _autologout_invalidate_other_sessions in Automated Logout 6.2

Same name and namespace in other branches
  1. 7.2 autologout.module \_autologout_invalidate_other_sessions()
1 call to _autologout_invalidate_other_sessions()
autologout_user in ./autologout.module
Implementation of hook_user().

File

./autologout.module, line 500
Used to automagically log out a user after a preset time, AjK May 2006

Code

function _autologout_invalidate_other_sessions($account) {

  // Do nothing if anonymous.
  if ($account->uid == 0) {
    return;
  }

  // check to see if the user is already logged in somewhere else
  // if so deactivate that login and let the user know that the
  // other session has been deactivated
  $sql = "SELECT COUNT(*) as count FROM {sessions} WHERE uid = '%s' AND sid <> '%s'";
  $result = db_result(db_query($sql, $account->uid, session_id()));
  if ($result != 0) {

    // Send the message to the current session
    $current_session_msg = t('You are only allowed 1 open session at a time. Your other session has been terminated.');
    drupal_set_message($current_session_msg, 'error');

    // Logout OTHER sessions, and send them their message
    $other_session_msg = t('You have been automatically logged out.  You are only allowed 1 open session at a time, and another open session was detected.');
    $other_session_msg = 'messages|' . serialize(array(
      'error' => array(
        $other_session_msg,
      ),
    ));
    db_query("UPDATE {sessions} SET uid='0', session = '%s' WHERE uid = '%s' AND sid <> '%s'", $other_session_msg, $account->uid, session_id());

    // Write a watchdog message for the site admin.
    watchdog('Automated Logout', t('One Session automatically logged out user.'), array(), WATCHDOG_WARNING);
  }
}