使用已发布变量cant检索其值的codeigniter cookie

时间:2014-06-25 05:52:13

标签: php codeigniter cookies

在我的索引页面中,我有一个输入字段来输入国家或城市名称。然后我将它发布到控制器,然后我将它设置为cookie和会话(用于测试)。

$visitingPlace = $this->input->post('place_visiting');
        $timestamp = now() + random_string('numeric', 5);
        $cookie[$timestamp] = array(
            'name' => 'searched_places',
            'value' => $visitingPlace,
            'expire' => '5184000',
        );
        $this->session->set_userdata($cookie);
        $this->input->set_cookie($cookie);

然后加载搜索结果页

$this->load->view('header', $data);
        $this->load->view('top');
        $this->load->view('hotel_search_view');
        $this->load->view('footer');

在搜索结果页面中,我试图获取cookie值,但它总是返回bool false

var_dump($this->input->cookie());
var_dump($this->session->all_userdata());

但是会话显示了所有细节。

bool(false) 

array(14) { ["session_id"]=> string(32) "4e940b327077d5ac23aea4ed8bef8a0a" ["ip_address"]=> string(3) "::1" ["user_agent"]=> string(109) "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36" ["last_activity"]=> int(1403675396) ["user_data"]=> string(0) "" [1403674268]=> array(3) { ["name"]=> string(15) "searched_places" ["value"]=> string(3) "sss" ["expire"]=> string(7) "5184000" } [1403674380]=> array(4) { ["name"]=> string(15) "searched_places" ["value"]=> string(4) "vila" ["expire"]=> string(7) "5184000" ["domain"]=> string(25) ".http://localhost/holasun" } [1403697663]=> array(4) { ["name"]=> string(15) "searched_places" ["value"]=> string(3) "sss" ["expire"]=> string(7) "5184000" ["domain"]=> string(25) ".http://localhost/holasun" } [1403754720]=> array(3) { ["name"]=> string(15) "searched_places" ["value"]=> string(5) "dsdas" ["expire"]=> string(7) "5184000" } [1403753967]=> array(3) { ["name"]=> string(15) "searched_places" ["value"]=> string(4) "sfdf" ["expire"]=> string(7) "5184000" } [1403770773]=> array(3) { ["name"]=> string(15) "searched_places" ["value"]=> string(3) "sss" ["expire"]=> string(7) "5184000" } [1403727272]=> array(3) { ["name"]=> string(15) "searched_places" ["value"]=> string(5) "sdfdf" ["expire"]=> string(7) "5184000" } [1403732164]=> array(3) { ["name"]=> string(15) "searched_places" ["value"]=> bool(false) ["expire"]=> string(7) "5184000" } [1403754757]=> array(3) { ["name"]=> string(15) "searched_places" ["value"]=> string(2) "sd" ["expire"]=> string(7) "5184000" } }

2 个答案:

答案 0 :(得分:1)

$this->input->cookie()要求您输入名称以检索数据,例如

 $this->input->cookie($timestamp); //for example

 $this->input->cookie('searched_places'); //for example

答案 1 :(得分:0)

这是您在Cookie中保存数据的方式:

<?php

$visitingPlace = $this->input->post('place_visiting');
$cookie = array(
    'name'   => 'searched_places',
    'value'  => $visitingPlace,
    'expire' => '5184000',
    'path'   => '/'
);
$this->input->set_cookie($cookie);

以下是从Cookie中检索数据的方法:

<?php

$cookie_value = $this->input->cookie('searched_places');

这是您在会话中保存数据的方式:

<?php

$visitingPlace = $this->input->post('place_visiting');
$this->session->set_userdata('searched_places', $visitingPlace);

这是您从会话中检索数据的方式:

<?php

$session_value = $this->session->userdata('searched_places');

对于Cookie read the manual。 对于会话read the manual。请务必阅读手册!