Codeigniter:会话临时数据在会话销毁时被删除


Codeigniter: session tempdata is removed on session destroy

我只需要在一个页面上显示一些数据20分钟。为此,我在codeigniter 中使用tempdata

实际上,tempdata是一个会话数据,我使用mark_as_temp方法将其标记为tempdata

这是我的代码

 public function final_result()
    {
    //make the session data as tempdata    
        $this->session->mark_as_temp(
          array('hotel_basic','user_ht_bk_data','hotel_info','hotel_search_query','booking_response','ht_star_rating','each_rooms'),1200       
        );     
      //after marking as tempdata destroy the original sessiondata
      $this->session->sess_destroy();
     //read from the tempdata
      $data['result']=$this->session->tempdata('user_ht_bk_data');
      $this->view('final-view',$data);
    }

但是$data['result']将返回空值。

根据代码点火器文档sess_destroy(),切勿删除临时数据。

但在我的情况下,tempdata在执行会话销毁时被删除

一旦使用sess_destroy(),所有会话数据(,包括flashdata和tempdata)都将被永久销毁,并且在销毁会话后的同一请求期间功能将不可用

销毁代码点火器中的会话

解决方案

  1. 照常显示最终结果(不销毁会话)并制作使用JavaScript倒计时,在20分钟内摧毁会话
  2. 使用Cookie

这是因为要设置为tempdata的数据必须事先在会话中退出。如文档中所述,您需要尝试以下操作:

$_SESSION['user_ht_bk_data'] = 'Test string'; // Or use set_tempdata with $this->session->set_tempdata('user_ht_bk_data', 'Test string', 300);
$this->session->mark_as_temp( 'user_ht_bk_data', 1200 ); // No need to use this if you used set_tempdata();
$this->session->sess_destroy();
$data['result']=$this->session->tempdata('user_ht_bk_data');