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