How to recover Codeigniter session data directly from database?

Asked

Viewed 1,261 times

3

I am trying to recover data from my table ci_sessions of CodeIgniter that are saved in a serialized way.

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

And I get that mistake:

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

Some way around it?

  • Why not use the Codeigniter library?

  • Because I want to take the data straight from the table, turn it into array and make comparisons. It’s not just using the current session, I want to recover old.

  • But in the table you will always have the most current Session of a given client. The other lines refer to other requests. You want to compare the data of different customers, that’s it?

  • In fact, of the same, to be able to inform to him, where he is logged.

1 answer

1

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

$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 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 a helper for the job 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;     
}
  • very good answer, but my biggest problem is that when taking the data and giving a unserialize() PHP gives me an error that says the format of the serial data is wrong and does not convert. Codeigniter has its own way of passing your data to array, but I didn’t succeed in using it either.

  • But tell me, I saw that on your call, you made a where array('id' => $id)), I didn’t quite understand this field in your session table. And yet, what comes of result?

  • Codeigniter session generates a cookie with a single value: id, which is the session id, the same that is saved in DB and which the CI uses to restore sessions. My output, if any, is a table row.

  • get_where('ci_sessions', array('id' => $id) Matches: SELECT * FROM ci_sessions WHERE id = '.$id.';

Browser other questions tagged

You are not signed in. Login or sign up in order to post.