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: vb开发者_运维技巧ool=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=
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.
精彩评论