Merge pull request #6490 from rxu/ticket/16470

[ticket/16470] Update user last visit time on session begin
This commit is contained in:
Marc Alexander 2023-11-04 08:26:45 +01:00
commit fe84120bf2
No known key found for this signature in database
GPG key ID: 50E0D2423696F995

View file

@ -440,6 +440,12 @@ class session
// Is user banned? Are they excluded? Won't return on ban, exists within method
$this->check_ban_for_current_session($config);
// Update user last visit time accordingly, but in a minute or so
if ((int) $this->data['session_time'] - (int) $this->data['user_lastvisit'] > 60)
{
$this->update_user_lastvisit();
}
return true;
}
}
@ -684,14 +690,11 @@ class session
{
$this->session_id = $this->data['session_id'];
// Only update session DB a minute or so after last update or if page changes
if ($this->time_now - $this->data['session_time'] > 60 || ($this->update_session_page && $this->data['session_page'] != $this->page['page']))
// Only sync user last visit time in a minute or so after last session data update or if the page changes
if ((int) $this->data['session_time'] - (int) $this->data['user_lastvisit'] > 60 || ($this->update_session_page && $this->data['session_page'] != $this->page['page']))
{
// Update the last visit time
$sql = 'UPDATE ' . USERS_TABLE . '
SET user_lastvisit = ' . (int) $this->data['session_time'] . '
WHERE user_id = ' . (int) $this->data['user_id'];
$db->sql_query($sql);
$this->update_user_lastvisit();
}
$SID = '?sid=';
@ -824,16 +827,13 @@ class session
{
$this->data['session_time'] = $this->data['session_last_visit'] = $this->time_now;
// Update the last visit time
$sql = 'UPDATE ' . USERS_TABLE . '
SET user_lastvisit = ' . (int) $this->data['session_time'] . '
WHERE user_id = ' . (int) $this->data['user_id'];
$db->sql_query($sql);
$SID = '?sid=';
$_SID = '';
}
// Update the last visit time
$this->update_user_lastvisit();
$session_data = $sql_ary;
/**
* Event to send new session data to extension
@ -960,8 +960,8 @@ class session
}
/**
* Get expired sessions for registered users, only most recent for each user
* Inner SELECT gets most recent expired sessions for unique session_user_id
* Get most recent session for each registered user to sync user last visit with it
* Inner SELECT gets most recent sessions for each unique session_user_id
* Outer SELECT gets data for them
*/
$sql_select = 'SELECT s1.session_page, s1.session_user_id, s1.session_time AS recent_time
@ -969,8 +969,7 @@ class session
INNER JOIN (
SELECT session_user_id, MAX(session_time) AS recent_time
FROM ' . SESSIONS_TABLE . '
WHERE session_time < ' . ($this->time_now - (int) $config['session_length']) . '
AND session_user_id <> ' . ANONYMOUS . '
WHERE session_user_id <> ' . ANONYMOUS . '
GROUP BY session_user_id
) AS s2
ON s1.session_user_id = s2.session_user_id
@ -1797,4 +1796,20 @@ class session
{
return isset($this->data['user_id']) ? (int) $this->data['user_id'] : ANONYMOUS;
}
/**
* Update user last visit time
*/
public function update_user_lastvisit()
{
global $db;
if (isset($this->data['session_time'], $this->data['user_id']))
{
$sql = 'UPDATE ' . USERS_TABLE . '
SET user_lastvisit = ' . (int) $this->data['session_time'] . '
WHERE user_id = ' . (int) $this->data['user_id'];
$db->sql_query($sql);
}
}
}