普通网友 2016-11-29 16:36
浏览 51
已采纳

在CodeIgniter中启动新会话

I have a session that I turn into a variable $userID.

I now need to use the variable within a different controller. I have tried to start a new session after the session $this->session->sess_destroy();.

Then on my second controller I have tried to get the session data but I am getting NULL.

First controller

public function index()
{

    if ($this->input->post('tid')) {

        $this->session->userdata('user_id');
        $userID = $this->session->userdata('user_id');
        $this->session->sess_destroy();

        $this->session->set_userdata($userID);

        $this->input->post('userid');
        $data['tid'] = $this->input->post('tid');
        $this->load->view('client/login', $data);


    } else {
        header("Location: " . base_url() . "index.php/select");
        die();
    }

}

Second controller

private function getResults()
{
    $this->session->userdata('user_id');

    $userID = $this->session->userdata('userid');

    var_dump($userID);
}

If I vardump the variable in my first controller after session destroy, the variable prints out. However it does not on the second.

  • 写回答

1条回答 默认 最新

  • douduonang3169 2016-11-29 18:13
    关注

    I'm not sure this is your problem but I think there's a problem on this line:

    $this->session->set_userdata($userID);
    

    set_userdata accepts two kinds of arguments (link to docs) :

    1. a single argument as an associative array of keys and values or
    2. a "key" first argument and a "value" second argument

    So what you're probably looking to do is rather

    $this->session->set_userdata('user_id', $userId);
    

    or

    $data = ['user_id' => $userId];
    $this->session->set_userdata($data);
    

    As you prefer. Also, as a side note, this kind of code gets repeated a few times :

    $this->session->userdata('user_id');
    $userID = $this->session->userdata('user_id');
    

    The first call is useless and should be removed.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀