How to retrieve data from the CodeIgniter session directly from the database?

3

I'm trying to retrieve data from my table ci_sessions of CodeIgniter that are serialized.

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

And I get this error:

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

Any way around this?

    
asked by anonymous 18.05.2015 / 21:15

1 answer

1

What I did once was to get the data from the ci_session table where user_data was different from empty and list these data, I know it's not quite what you're looking for, but I think it should give you a light. / p>

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

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

and in the view I have listed the active sessions:

<table>
    <thead>
        <tr>
            <th>IP ADDRESS</th>
            <th>USER AGENT</th>
            <th>LAST ACTIVITY</th>
            <th>USER DATA</th>                
        </tr>
    </thead>
    <tbody>

    <?php
    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";
    endforeach;
    ?>

    </tbody>
</table>

and I have helper for the functions get_timestamp_to_time and get_dados_user_data :

function get_timestamp_to_time($timestamp)
{
    if ($timestamp) {
        $date = new DateTime();
        $date->setTimestamp($timestamp);
        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;     
}
    
19.05.2015 / 16:21