codeigniter会话问题 - 一些会话信息不坚持

时间:2011-01-25 15:21:37

标签: session codeigniter

我正在使用codeigniter会话库来保存一系列3页中使用的数据,而且我遇到了奇怪的行为。我的会话变量保持不变,但价值消失。更奇怪的是:我正在尝试在我的会话数据中存储一个序列化数组,并且数组的第一项最终存储在另一个变量中?

我附上了一个链接,该链接从系列的第一页开始,可以点击下一页。我在两个页面的顶部打印了user_session数据(第三页尚未设置)。

http://playmatics.com/nypl/site/index.php/member_area/quest/accept_quest/12

会话在其他地方工作,例如我正在使用会话来存储登录数据,并且工作正常。

我已连接我的控制器并在下面查看

    //CONTROLLER:
    function accept_quest() {
        $assoc_quest_id = end($this->uri->segments);

        if(!isset($quest_id)) {
            redirect('member_area/quest');
            //SEND A MESSAGE: NO QUEST STARTED
        }

        $quest_rows = $this->quest_model->get_quest_with_images($assoc_quest_id);
        $quest = current($quest_rows);
        $images = $this->pull_out_images($quest_rows);

        //the data array is used both in the session, 
        //to pass values over to the next function in the quest chain
        //and in the template
        $data = array();
        $data['quest_id'] = $assoc_quest_id;
        $data['instruction_text'] = $quest->instructions;
        $data['quest_title'] = $quest->name;
        $data['quest_time_limit'] = $quest->time_limit;
        $data['points_awarded'] =  $quest->points_availible;
        $data['quest_images'] = serialize($images);

       //save data in a flash session to be used in the next function call in the quest chain: quest_action
       $this->session->set_userdata($data);
       print_r($this->session->all_userdata());


       //the following data aren't needed in the session so they are added to the data array after the session has been set
       $data['annotation_text'] = $quest->note;        
       $data['main_content'] = 'quests/quest_desc';
       $this->load->view('includes/template', $data);
}


function quest_action() {
    print_r($this->session->all_userdata());

    $quest_id = $this->session->userdata('quest_id');
    echo "the quest id is: $quest_id";
    if(!isset($quest_id)) {
        redirect('member_area/quest');
        //SEND A MESSAGE: NO QUEST STARTED
    }

    $data['quest_id'] = $quest_id;
    $data['quest_title'] = $this->session->userdata('quest_title');
    $data['quest_images'] = $this->session->userdata('images');
    $data['instruction_text'] = $this->session->userdata('instructions');
    $data['quest_time_limit'] = $this->session->userdata('quest_time_limit');

    $data['main_content'] = 'quests/quest_action';
    $this->load->view('includes/template', $data);                
}

//VIEW
   //quest_desc:
<h1><?= $quest_title ?></h1>
    <div id="quest_elements">
        <figure>
        <? foreach(unserialize($quest_images) as $image): ?>
            <img class="media" src="<?= $image ?>" alt="<?= $quest_title ?> image"/>
        <? endforeach; ?>
            <figcaption>annotation: <?= $annotation_text ?></figcaption>
        </figure>
        <?= anchor("member_area/quest/quest_action", "Start Quest", array('title' => 'start quest')); ?>
    </div><!-- end quest_elements -->


    //quest_action:
<h1><?= $quest_title ?></h1>
    <div id="quest_elements">
        <figure>
        <? foreach(unserialize($quest_images) as $image): ?>
            <img class="media" src="<?= $image ?>" alt="<?= $quest_title ?> image"/>
        <? endforeach; ?>
            <figcaption>instructions: <?= $instruction_text ?></figcaption>
        </figure>
    <div id="timer">
        <?= $quest_time_limit; ?>
    </div>
    <?= anchor("#start_timer", "Start Timer", array('title' => 'start quest timer')); ?>
</div>

2 个答案:

答案 0 :(得分:3)

如果您达到cookie大小限制,我建议您切换到CodeIgniter的原生Database Sessions class。这使您可以将会话信息存储在数据库中,有效地消除了cookie大小限制,您只需将其限制为user_data数据库中ci_sessions字段的大小。

按照上面的链接,关于利用数据库会话的部分接近底部,为您提供正确的数据库架构和配置切换到数据库会话。

答案 1 :(得分:2)

正如其他人所说的那样,很可能你正在达到CI会话库的4k cookie限制。还有其他可用的替代库使用标准PHP会话 - 例如http://codeigniter.com/wiki/PHPSession/http://codeigniter.com/wiki/Native_session/