duanliang789262 2014-06-25 05:52
浏览 35
已采纳

使用已发布变量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 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!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?