CodeIgniter Forums
session set_userdata does not preserve data types over a redirect in 1.7.3 (works in 1.6.1) - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: session set_userdata does not preserve data types over a redirect in 1.7.3 (works in 1.6.1) (/thread-41502.html)



session set_userdata does not preserve data types over a redirect in 1.7.3 (works in 1.6.1) - El Forum - 05-09-2011

[eluser]Unknown[/eluser]
In CodeIgniter 1.7.3 when you use set_userdata to add boolean, integer, and string values to your session, and then immediately read them back with session->userdata(), 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?

In this example the session is not stored in database.

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 (expect boolean)
ERROR - 2011-05-09 16:56:11 --> test2: vint=23 string (expect integer)
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=

thanks for your help!
Tom