代码Igniter Cookie [英] Code Igniter Cookie

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

问题描述

我是codeigniter的新手。处理Cookie时遇到一些问题。最初,我已将Cookie值设置为0.当用户导航到下一页时,我想将Cookie值增加1。我的控制器是这样

I am a newbie to codeigniter. 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);

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

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