Welcome Guest, Not a member yet? Register   Sign In
helper function to get logged in users whose sessions have not expired
#1

(This post was last modified: 10-02-2021, 10:08 PM by dgvirtual.)

In managing and using one project I find I need to know if no user is active in the live website, as they might be entering data that I might interrupt. Then I know I can turn on the maintenance mode and do updates to the website.
So I have written this helper function that is perhaps IonAuth- specific, but that helps me to find out that data. You can put it into a file in the helper directory (app/Helpers), load it into your project as a helper, and get the list of active users this way:
PHP Code:
$active_users active_sessions(); 
Here is the function itself. If anyone has suggestions how to make it less dependent on IonAuth and more error-prone, please let me know. Hope it will be useful. I have commented it extensively to make the sphagetti-code more clear.
PHP Code:
function active_sessions()
{
    $config config('App');
    helper('filesystem');
    $map directory_map(WRITEPATH '/session');
    //get names and dates of the session files
    foreach ($map as $file) {
        $file_data[] = get_file_info(WRITEPATH '/session/' $file, ['name''date']);
    }
    
    
// set the variables to be used: 
    //get timestamp before which the sessions are expired
    //current time minus the session expiration time
    $session_threshold time() - $config->sessionExpiration
    //last movement within system variable
    $last_movement $session_threshold;
    // array of active users
    $active_users = array();
    //cycle through the file list
    foreach ($file_data as $key => $value) {
        if ($value['date'] > $session_threshold) {
            //read file contents
            // they look something like, must be ion_auth-specific:
            // '__ci_last_regenerate|i:1633202933;_ci_previous_url|s:34:"http://site.com/page";identity|s:13:"[email protected]";email|s:13:"[email protected]";user_id|i:44;old_last_login|i:1633114505;last_check|i:1633197212;%
            $filec file_get_contents(WRITEPATH '/session/' $value['name'], true);
            //exclude cases which include your own email and which do not include the email variable:
            if (!strpos($filec$_SESSION['email']) && strpos($filec'email')) {
                // in case of a match, explode the file and get the email variable
                $data explode("\""$filec);
                foreach ($data as $datum) {
                    if (strpos($datum"@")) {
                        $active_users[$datum] = $datum//this way only unique emails are preserved
                        // if the timestamp of the file is newer than the current in variable
                        // replace old with new; the end result should be the last
                        // user movement time
                        if ($last_movement $value['date']) {
                            $last_movement $value['date'];
                        }
                        break;
                    }
                }
            };
        }
    }
    //set the data to return
    $data['users'] = $active_users//list of user emails
    $data['last_movement'] = gmdate("Y-m-d H:i:s"$last_movement date("Z")); //last movement
    return $data;

==

Donatas G.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB