3

在 CodeIgniter 1.7.3 中,当您使用 set_userdata 添加布尔值、整数和字符串值,然后立即将它们读回时,类型会被保留。但是如果你重定向到另一个页面并读回这些值,你总是会得到字符串值。在 CI 1.6.1 中,类型将被保留。任何想法为什么会发生这种情况?它是 1.7.3 中的错误吗?任何解决方法?

示例:运行 test1 设置会话数据,将其读回,重定向到 test2,然后再次读回:

<?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));
    }

}
?>

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

配置设置

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=
4

1 回答 1

1

我在 CodeIgniter 2.0.0 上尝试了相同的测试,它运行良好——当使用 userdata() 读回会话数据时,使用 set_userdata() 存储在会话中的数据类型被保留。所以这似乎是 CI 1.7.3 存储会话数据时的一个错误,然后进行重定向,然后读取会话数据。

于 2011-05-11T06:38:12.623 回答