【问题标题】:Codeigniter: session tempdata is removed on session destroyCodeigniter:会话临时数据在会话销毁时被删除
【发布时间】:2016-03-25 13:04:26
【问题描述】:

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

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

这是我的代码

 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'] 会返回空值。

根据 codeigniter 文档sess_destroy() 永远不要删除临时数据。

但在我的情况下,tempdata 在执行 session_destroy 时被删除

【问题讨论】:

标签: php codeigniter session


【解决方案1】:

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

Destroying a Session In Codeigniter

解决方案

  1. 照常显示最终结果(不破坏会话)并制作 countdown with JavaScript 在 20 分钟内销毁会话。
  2. Use cookies

【讨论】:

  • @shammon 检查答案
  • @shammon 乐于助人 :)
【解决方案2】:

那是因为要设置为临时数据的数据必须事先已经在会话中退出。作为stated in the documentation,您需要尝试以下操作:

$_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');

【讨论】:

  • 同样是我使用的唯一区别是mark_as_temp 中的一个参数是一个数组
  • 我没有看到您的代码添加会话数据。请发布所有相关代码,其他地方可能会有影响。
猜你喜欢
  • 2016-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-27
  • 2012-01-04
  • 2019-01-26
  • 2016-11-19
相关资源
最近更新 更多