【发布时间】:2014-01-09 22:17:39
【问题描述】:
我无法掌握使用 codeigniter 加载 cookie 的时间和方式。访问这个控制器会调用一个 index 函数,然后它会根据它是否已经被设置创建一个 cookie 并加载另一个函数来加载视图,如下所示:
public function index($id){
$this->load->model('test_model');
if($this->input->cookie('cookie'.$id) == false || null){
$this->test_model->setCookie(id);
$this->testView($id);
} else {
// echo $temp;
$this->testView(id);
}
}
设置cookie代码:
function setCookie($id){
$cookie = array(
'name' => "cookie".$id,
'value' => 0,
'expire' => '86500',
);
$this->input->set_cookie($cookie);
}
在我看来,我有一个表单,我尝试更新 cookie 的内容,如下所示:
public function FormSubmission(){
$temp = $this->input->cookie("cookie".$id, TRUE);
echo $temp;
$temp2 = $this->test_model->updateCookie();
echo $temp2;
redirect('temp/index'.$id);
}
updateCookie() 函数:
function updateCookie(){
$id = $this->input->post('id');
$pointer = $this->input->cookie("cookie".$id, TRUE);
$pointer = $pointer + 1;
delete_cookie("cookie".$id);
$cookie = array(
'name' => "cookie".$id,
'value' => $pointer,
'expire' => '86500',
);
$this->input->set_cookie($cookie);
}
当我在更新前回显 cookie 时,它显示为 0。更新后它显示为 0,然后我重定向到索引函数。抱歉,如果这真的很简单,我是一个完全的新手。
【问题讨论】:
标签: php codeigniter cookies