fetchAdminSessions (function)

Last updated: Fri, Apr 14, 2023 5:04 pm
Return to Knowledgebase

Purpose

Retrieves user session data from the database.

Location

users/helpers/us_helpers.php

Parameters

# Parameter Data Type Required Description
1 $all = false bool No A boolean value that specifies whether to retrieve all sessions (if true) or only active sessions (if false).

Returns

Data Type Description of Returned Data
other If the query returns one or more rows, the function returns the results as an array of objects. Each object represents a session and contains properties corresponding to the columns in the "us_user_sessions" table. If the query returns no rows, the function returns false to indicate that no sessions were found.

Further Documentation:

The function takes one optional parameter: $all, which is a boolean value that specifies whether to retrieve all sessions (if true) or only active sessions (if false).

The function first sets the global variable $user, which is an instance of the User class that provides access to user-related functionality.

The function then gets an instance of the database class using the DB::getInstance() method, which creates a new PDO object and establishes a connection to the database.

If $all is false, the function executes a query to select all rows from the "us_user_sessions" table where the "UserSessionEnded" column is 0 (i.e., the session is still active). The rows are ordered by the "UserSessionStarted" column, which contains the start time of each session.

If $all is true, the function executes a similar query but selects all rows from the table, regardless of whether the session is active or not.

// Retrieve all active sessions
$sessions = fetchAdminSessions();

// Display session data in a table
if ($sessions) {
echo '';
echo '';
foreach ($sessions as $session) {
echo '';
echo '';
echo '';
echo '';
echo '';
echo '';
}
echo '
Session IDUser IDStart TimeLast Activity
'.$session->UserSessionID.''.$session->UserSessionUserID.''.$session->UserSessionStarted.''.$session->UserSessionLastActivity.'
';
} else {
echo 'No active sessions found.';
}