Search code examples
phpcodeigniter-3

Codeigniter 3 Show All Online User


I am using database level session store method.

$config['sess_driver'] = 'database';
$config['sess_save_path'] = 'ci_sessions';//its your table name name

So when user login data inserted into ci_sessions successfully.

__ci_last_regenerate|i:1435420891;identity|s:13:"john@doe.com ";username|s:13:"johndoe";email|s:13:"john@doe.com ";user_id|s:1:"5";old_last_login|s:10:"1435412865";

Like this, this table may contains multiple records. I want to show all the online user. This data column has blob type. How can I fetch all the username?


Solution

  • I get information using following code.

    $session_data   = '__ci_last_regenerate|i:1435420891;identity|s:13:"john@doe.com ";username|s:13:"johndoe";email|s:13:"john@doe.com ";user_id|s:1:"5";old_last_login|s:10:"1435412865"'; //session data
    
    $return_data    = array();  // array where you put your "BLOB" resolved data
    $offset         = 0;
    while ($offset < strlen($session_data)) {
        if (!strstr(substr($session_data, $offset), "|")) {
            throw new Exception("invalid data, remaining: " . substr($session_data, $offset));
        }
        $pos        = strpos($session_data, "|", $offset);
        $num        = $pos - $offset;
        $varname    = substr($session_data, $offset, $num);
        $offset     += $num + 1;
        $data       = unserialize(substr($session_data, $offset));
        $return_data[$varname] = $data;  
        $offset     += strlen(serialize($data));
    }
    
    echo $return_data['username'];
    echo $return_data['email'];