CodeIgniter Cookie [英] CodeIgniter Cookie

查看:59
本文介绍了CodeIgniter Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在处理Cookie时遇到了一些麻烦.最初,我将cookie值设置为0.当用户导航到下一页时,我想将cookie值增加一.我的控制器是这样的:

I'm having some trouble handling cookies. Initially I have set the cookie value to 0. When a user navigates to next page, I want to increment the cookie value by one. My controller is like this:

class Welcome extends CI_Controller {

    public function index() {
        $this->load->helper('cookie');
        $this->input->set_cookie("starttime", time(),time()+3600);
        $this->input->set_cookie("pagevisited",0,time()+3600);
        $_SESSION['currenttime'] = time();
        $this->load->view('indexpage');
    }

    public function page1() {
        $this->load->helper('cookie');
        $value = $this->input->cookie("pagevisited");
        $this->input->set_cookie("pagevisited",$value+1,time()+3600);
        $this->load->view('page1');
    }

    public function page2() {
        $this->load->helper('cookie');
        $value = $this->input->cookie("pagevisited");
        $this->input->set_cookie("pagevisited",$value+1,time()+3600);
        $this->load->view('page2');
    }
}

上面的代码不起作用.cookie值仍然为0.我注意到CI还在存储具有相同cookie名称的会话变量.

The above code is not working. The cookie value is still 0. I noticed that CI is also storing session variables with the same cookie name.

推荐答案

我认为您的cookie语法不正确.我相信CodeIgniter手册只说名称和值是必需的,但是要添加其他参数,您需要全部设置它们或定义一个数组.这是定义所需Cookie的两种方法.

I think your cookie syntax isn't correct. The CodeIgniter manual says only the name and value are required, but to add additional parameters you need to set them all or define an array, I believe. Here are two methods of defining the cookie you want.

$cookie = array(
    'name'   => 'The Cookie Name',
    'value'  => 'The Value',
    'expire' => '86500'
);

$this->input->set_cookie($cookie);

$this->input->set_cookie($name, $value, $expire, $domain, $path, $prefix, $secure);

这篇关于CodeIgniter Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆