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 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答