0
votes

i would like to extend the Session.php library in Codeigniter.

I would like to use a base64 encoded/decoded session values.

So basically i would like to encode globally all session data before it is setted (session->set_userdata()) and then decode that when is returned (session->userdata('item'))

is it possible?

I'm in trouble how do i fire the moment that the session data is gonna be setted and the moment when the data is gonna be returned?

I'm using the Codeigniter session DB and planning to write MY_Session.php library but can't go on, i'm blocked here:

class MY_Session extends CI_Session{

}

any help appriciated, thanks

1
You simply need to override the appropriate method for writing/reading data to include you encoding. I would wonder why the need for encoding as you are going to be adding some overhead to the process. - Mike Brant
@MikeBrant cause i need to support multibytes strings inside, and the default lib crashes with them :( - bombastic

1 Answers

1
votes

You need to override the session library's implementations of session->set_userdata() and session->userdata().

class MY_Session extends CI_Session{
    public function set_userdata($data, $singleVar = NULL) {
        if(is_array($data)) {
            foreach($data as $key => &$value) {
                //Encode $value
            }
            parent::set_userdata($array);
        }
        else {
            //Encode $singleVar 
            parent::set_userdata($data, $singleVar);
        }
    }

    public function userdata($item) {
        $data = parent::userdata($item);
        //Decode $data
        return $data;
    }
} 

Your new functions will be publically callable instead of the functions in the session class, and by calling parent:: you can access the function in the Codeigniter library.