killSessions (function)

Last updated: Mon, Apr 17, 2023 12:32 pm
Return to Knowledgebase

Purpose

Used to end a user session or multiple user sessions in the database.

Location

users/helpers/us_helpers.php

Parameters

# Parameter Data Type Required Description
1 $sessions array Yes array of session IDs that should be ended
2 $admin = false bool No indicates whether the function is being called by an administrator or not

Further Documentation:

The killSessions function in UserSpice is used to end a user session or multiple user sessions in the database. This function takes two parameters, $sessions and $admin, where $sessions is an array of session IDs that should be ended, and $admin is a boolean value that indicates whether the function is being called by an administrator or not.

When the $admin parameter is set to false, the function will only end the sessions for the current user by updating the UserSessionEnded and UserSessionEnded_Time fields in the us_user_sessions table. On the other hand, when $admin is set to true, the function will end the sessions for all users whose session IDs are included in the $sessions array.

The killSessions function also logs the details of the session(s) that have been ended using the logger function, and it returns the number of sessions that have been successfully ended or false if no sessions were ended.

Here's an example of how to use the killSessions function to end a user session with a given session ID:


// Load UserSpice
require_once 'userspice/init.php';

// Set the session ID to be ended
$session_id = 123;

// Call the killSessions function to end the session
$sessions_ended = killSessions([$session_id]);

// Check if the session was successfully ended
if ($sessions_ended) {
echo "Session ended successfully.";
} else {
echo "Failed to end session.";
}



In the example above, the killSessions function is called with an array containing only one session ID. The function will then attempt to end the session with that ID, and return the number of sessions that have been successfully ended. If the session was successfully ended, the code will output "Session ended successfully." Otherwise, it will output "Failed to end session."