Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

codeigniter set_userdata not preserving types in session data

In CodeIgniter 1.7.3 when you use set_userdata to add boolean, integer, and string values, and then immediately read them back, the types are preserved. But if you redirect to another page and read back the values, you always get string values. In CI 1.6.1 the types would be preserved. Any ideas why this is happening? Is it a bug in 1.7.3? Any workarounds?

Example: run test1 to set session data, read it back, redirect to test2, and read it back again:

<?php
class Test1 extends Controller
{
    function index()
    {
        $this->session->set_userdata(array('vbool'=>TRUE));
        $this->session->set_userdata(array('vint'=>23));
        $this->session->set_userdata(array('vstr'=>'abc'));

        $vbool = $this->session->userdata('vbool');
        $vint = $this->session->userdata('vint');
        $vstr = $this->session->userdata('vstr');

        log_message('error', "test1: vbool=$vbool " . gettype($vbool));
        log_message('error', "test1: vint=$vint " . gettype($vint));
        log_message('error', "test1: vstr=$vstr " . gettype($vstr));

        redirect('/backend/test2', 'location');
    }
}
?>


<?php
class Test2 extends Controller
{

    function index()
    {
        $vbool = $this->session->userdata('vbool');
        $vint = $this->session->userdata('vint');
        $vstr = $this->session->userdata('vstr');

        log_message('error', "test2: vbool=$vbool " . gettype($vbool));
        log_message('error', "test2: vint=$vint " . gettype($vint));
        log_message('error', "test2: vstr=$vstr " . gettype($vstr));
    }

}
?>

OUTPUT in CI LOG

ERROR - 2011-05-09 16:56:11 --> test1: vbool=1 boolean
ERROR - 2011-05-09 16:56:11 --> test1: vint=23 integer
ERROR - 2011-05-09 16:56:11 --> test1: vstr=abc string

ERROR - 2011-05-09 16:56:11 --> test2: vbool=1 string
ERROR - 2011-05-09 16:56:11 --> test2: vint=23 string
ERROR - 2011-05-09 16:56:11 --> test2: vstr=abc string

CONFIG SETTINGS

ERROR - 2011-05-09 16:56:11 --> sess_encrypt_cookie=
ERROR - 2011-05-09 16:56:11 --> sess_use_database=
ERROR - 2011-05-09 16:56:11 --> sess_table_name=ci_sessions
ERROR - 2011-05-09 16:56:11 --> sess_expiration=7200
ERROR - 2011-05-09 16:56:11 --> sess_match_ip=
ERROR - 2011-05-09 16:56:11 --> sess_match_useragent=1
ERROR - 2011-05-09 16:56:11 --> sess_cookie_name=ci_session
ERROR - 2011-05-09 16:56:11 --> cookie_prefix=
ERROR - 2011-05-09 16:56:11 --> cookie_path=/
ERROR - 2011-05-09 16:56:11 --> sess_time_to_update=300
ERROR - 2011-05-09 16:56:11 --> encryption_key=
like image 351
TomBl Avatar asked Aug 13 '26 14:08

TomBl


1 Answers

I tried the same test on CodeIgniter 2.0.0 and it worked fine - the data types stored in the session with set_userdata() were preserved when reading back session data with userdata(). So this seems to be a bug with CI 1.7.3 when storing session data, then doing a redirect, then reading the session data.

like image 94
TomBl Avatar answered Aug 18 '26 01:08

TomBl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!