function login_history_user_login in Login History 8
Same name and namespace in other branches
- 7 login_history.module \login_history_user_login()
Implements hook_user_login().
File
- ./
login_history.module, line 13 - Records a history of the site's user logins.
Code
function login_history_user_login($account) {
$request = \Drupal::request();
// Is this a one-time login?
$one_time = \Drupal::routeMatch()
->getRouteName() === 'user.reset.login' ? 1 : 0;
// Limit user agent strings to 255 characters. For example, some versions of
// IE8 return long user agent strings causing an error upon login.
$user_agent = $request->server
->get('HTTP_USER_AGENT');
if (strlen($user_agent) > 255) {
$user_agent = substr($user_agent, 0, 255);
}
// Now save the user's current login timestamp to login_history.
\Drupal::database()
->insert('login_history')
->fields([
'uid' => $account
->id(),
'login' => $account
->getLastLoginTime(),
'hostname' => $request
->getClientIP(),
'one_time' => $one_time,
'user_agent' => $user_agent,
])
->execute();
}