在CodeIgniter中检索Cookie数据

时间:2014-02-26 08:16:01

标签: php codeigniter cookies

我有以下功能:

    public function check_daily_data() {
            $month = $this->input->post('month');
            echo 'Month :' . $month . '</br>';
            $year = $this->input->post('year');
            echo 'Year :' . $year . '</br>';
            $user_id = $this->session->userdata('employee_id');
            echo 'User Id : ' . $user_id . '</br>';
            //load the cookie helper
            $this->load->helper('cookie');

                setcookie("month", "", time()-3600);
            setcookie("year", "", time()-3600);
            $time = time()+3600;
            $cookie_month = array(
                'name' => 'cookie_month',
                'value' => $month,
                'expire' => '3600',
                'secure' => TRUE
            );
            $this->input->set_cookie($cookie_month);

            $cookie_year = array(
                'name' => 'cookie_year',
                'value' => $year,
                'expire' => '3600',
                'secure' => TRUE
            );
            $this->input->set_cookie($cookie_year);

            var_dump($this->input->cookie('cookie_month')); 
            $cookie_months = $this->input->cookie('cookie_month',TRUE);
            echo 'Cookie Month : ' . $cookie_months . '</br>';

            $cookie_years = $this->input->cookie('cookie_year',TRUE);
            echo 'Cookie Year : ' . $cookie_years . '</br>';
}

当我运行脚本并使用FireBug检查cookie变量时,我可以查看cookie数据,但是当我尝试回显它时,我得到一个空值。 var_dump返回false,你能告诉我我做错了什么吗?

1 个答案:

答案 0 :(得分:0)

好吧,为什么你没有获得价值是因为你设置了'secure' => TRUE。您需要将其设置为false。真实设置仅适用于HTTPS

$cookie_month = array(
                'name' => 'cookie_month',
                'value' => $month,
                'expire' => '3600',
                'secure' => FALSE
            );

 $cookie_year = array(
                'name' => 'cookie_year',
                'value' => $year,
                'expire' => '3600',
                'secure' => FALSE
            );

然后,当你打电话

时,你会得到你的价值
$cookie_months = $this->input->cookie('cookie_month',TRUE);