duanliang789262
2014-06-25 05:52 阅读 34
已采纳

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

In my index page I have a input field to enter a country or city name. then I post it to a controller and there I set it to a cookie and to a session (for testing).

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

and then load a search result page

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

In the search result page I am trying to get the cookie values, but it always return bool false

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

but the session is showing all the details.

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条回答 默认 最新

  • 已采纳
    dousou1967 dousou1967 2014-06-25 06:41

    This is how you save data in cookie:

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

    This is how you retrieve the data from cookie:

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

    This is how you save data in session:

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

    This is how you retrieve the data from session:

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

    For cookies read the manual. For sessions read the manual. Always read the manual!

    点赞 评论 复制链接分享
  • douben7260 douben7260 2014-06-25 05:57

    This $this->input->cookie() requires you to input a name to retrieve data, e.g,

     $this->input->cookie($timestamp); //for example
    
     $this->input->cookie('searched_places'); //for example
    
    点赞 评论 复制链接分享

相关推荐