mysql – How to retrieve CodeIgniter session data directly from database?


I'm trying to retrieve data from my CodeIgniter ci_sessions table that is saved ci_sessions .

$data = $this->db->get_where('ci_sessions', array('id' => $id))->row('data');

And I get this error:

// Message: unserialize(): Error at offset 0 of 135 bytes

Any way around this?


What I did once was get the data from the ci_session table where user_data was different from empty and listed this data, I know that's not quite what you're looking for, but I think it should give you a light.

$data['all_results'] = $this->ci_sessions_model->get("user_data <> ''");

$this->load->view('widget_sessions', $data);

and in the view I listed the active sessions:

            <th>IP ADDRESS</th>
            <th>USER AGENT</th>
            <th>LAST ACTIVITY</th>
            <th>USER DATA</th>                

    foreach ($all_results as $all):
        $id_user = get_dados_user_data($all->user_data, array('id_user'));

        echo "\t\t<tr id='" . $all->session_id . "'>\n";
        echo "\t\t\t<td>" . $all->ip_address . "</td>\n";
        echo "\t\t\t<td>" . $all->user_agent . "</td>\n";
        echo "\t\t\t<td>" . get_timestamp_to_time($all->last_activity) . "</td>\n";
        echo "\t\t\t<td>" . get_dados_user_data($all->user_data, array('str_nome', 'str_sobrenome', 'str_email')) . "</td>\n";
        echo "\t\t</tr>\n";


and I have a helper for the get_timestamp_to_time and get_dados_user_data functions:

function get_timestamp_to_time($timestamp)
    if ($timestamp) {
        $date = new DateTime();
        return $date->format('d/m/Y H:i:s');
    } else {
        return NULL;

function get_dados_user_data($userdata, $valuesReturn = array())
    if ($userdata) {
        $ret = "";
        $arUserData = decode_user_data($userdata);
        foreach ($arUserData as $key => $value) {
            if (in_array($key, $valuesReturn)) {
                $ret.= $value . " ";
        return $ret;

    return NULL;      

function decode_user_data($userdata)
    if ($userdata) {
        return unserialize($userdata);

    return NULL;     
Scroll to Top