我在我的控制器中使用它来检查是否已经设置了cookie。如果没有,我设置它。
function x(){
if(!isset($_COOKIE['sg'])){
$this->load->model('generate_model');
$val=$this->generate_model->random();
setcookie('sg',$val, time()+3600*24*30*12*3,"/", "" );
}
$this->load->model('model');
$data['cod']=$cod;
$this->model->select($cod);
$this->load->view('templates/header');
redirect('Home','refresh');
}
如果没有设置cookie,我会收到两个错误:
遇到PHP错误
严重性:注意
消息:未定义的索引:sg
文件名:models / select_gift_model.php
行号:12
发生数据库错误
错误号码:1048
列'uid'不能为空
INSERT INTO
id
(uid
,cod
)VALUES(NULL,'35A5V0Mogc')文件名:C:\ wamp \ www \ ci \ system \ database \ DB_driver.php
行号:330
设置了cookie,如果我按回并再次呼叫,则该功能正常工作。
如何检查是否设置了cookie并使该功能正常工作?
答案 0 :(得分:1)
在php的文档中:
Once the cookies have been set, they can be accessed on the next page load with the $_COOKIE or $HTTP_COOKIE_VARS arrays. Note, superglobals such as $_COOKIE became available in PHP 4.1.0. Cookie values also exist in $_REQUEST.
来源:http://php.net/manual/en/function.setcookie.php
使用当前连接的最佳方法是进行后续处理:
setcookie('sg',$val, time()+3600*24*30*12*3,"/", "" );
$_COOKIE = $val;