如何使用Codeigniter回显单个会话


How to echo a single session with Codeigniter?

我想阅读一个会话。

这就是我设置所有会话的方式:

$data = array(
    'connection'    => true,
    'sender_db'     => $this->input->post('sender_db'),
    'sender_host'   => $this->input->post('sender_host'),
    'sender_user'   => $this->input->post('sender_user'),
    'sender_pw'     => $this->input->post('sender_pw'),
    'receiver_db'   => $this->input->post('receiver_db'),
    'receiver_host' => $this->input->post('receiver_host'),
    'receiver_user' => $this->input->post('receiver_user'),
    'receiver_pw'   => $this->input->post('receiver_pw'),
);
foreach ($data as $key => $value) {
    $this->session->set_userdata($key, $value);
}

这就是我如何阅读会话sender_host:

var_dump($this->session->get_userdata('sender_host'));

好吧,这给了我一个数组,其中所有其他会话也存储在:

array(9) {
    ["connection"]=> bool(true)
    ["sender_db"]=> string(12) "datamigrator"
    ["sender_host"]=> string(9) "localhost"
    ["sender_user"]=> string(4) "root"
    ["receiver_db"]=> string(8) "anything"
    ["sender_pw"]=> string(0) ""
    ["receiver_host"]=> string(8) "anything"
    ["receiver_user"]=> string(8) "anything"
    ["receiver_pw"]=> string(8) "anything"
}

如何只进行一次会话?

我认为问题在于我如何设置会话?!

试试这个:

构建阵列

$data = array(
    'connection' => true,
    'sender_db' => $this->input->post('sender_db'),
    'sender_host' => $this->input->post('sender_host'),
    'sender_user' => $this->input->post('sender_user'),
    'sender_pw' => $this->input->post('sender_pw'),
    'receiver_db' => $this->input->post('receiver_db'),
    'receiver_host' => $this->input->post('receiver_host'),
    'receiver_user' => $this->input->post('receiver_user'),
    'receiver_pw' => $this->input->post('receiver_pw'),
);

设置会话:

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

从会话中这样阅读:

$this->session->userdata('sender_host');