[英]Not able to Set cookie in Code-igniter
I'm not able to generate cookie the below code works only for only else part. 我无法生成Cookie,以下代码仅适用于其他部分。
public function set(){
$cookie = array(
'name' => 'demo',
'value' => 'hello i m saved cookie',
'expire' => '86500'
);//EOF array
if($this->input->set_cookie($cookie))
{
$data = array( 'message' => 'cookie successfully set');
$this->load->view('cookies_view',$data);
}
else{
$data = array( 'message' => 'Something went wrong while creating cookie');
$this->load->view('cookies_view',$data);
}
Your value already stored in cookie, $this->input->set_cookie($cookie)
this will only create the cookie. 您的值已经存储在cookie中,
$this->input->set_cookie($cookie)
只会创建cookie。 If you want to check cookie value set or not than you can use like that: 如果您要检查cookie值集,则可以这样使用:
$cookie = array(
'name' => 'demo',
'value' => 'Hello i m cookies which saved in this broswer',
'expire' => '86500',
);
$this->input->set_cookie($cookie);
if(isset(get_cookie('demo'))){ // check cookie value
echo "success"; // replace with your code
}
else{
echo "failed"; // replace with your code
}
get_cookie('demo')
will return the cookie value. get_cookie('demo')
将返回cookie值。
You can also explore the CI Manual . 您也可以浏览CI手册 。
Make sure, you are using cookie helper in your file, you must need to include cookie helper: 确保您在文件中使用cookie助手,必须包含cookie助手:
$this->load->helper('cookie');
$this->input->set_cookie($cookie);
this function returns NULL thats why your condition is not working fine. 此函数返回NULL,这就是为什么您的条件无法正常工作的原因。 your cookie is being set
您的Cookie正在设置
use $this->input->cookie('your cookie name')
to check your condition 使用
$this->input->cookie('your cookie name')
检查您的状况
public function set()
{
$this->load->helper('cookie');
$cookie = array(
'name' => 'demo',
'value' => 'Hello i m cookie',
'expire' => '86500'
);
$this->input->set_cookie($cookie);
if ($this->input->cookie('demo')) {
$data['data'] = array('message' => 'cookie successfully set');
$this->load->view('your view', $data);
} else {
$data['data'] = array('message' => 'Something went wrong while creating cookie');
$this->load->view('your view', $data);
}
}
In my case after hours of debugging I realized that cookie_secure was set to true for local development. 在经过数小时的调试后,我意识到对于本地开发,cookie_secure设置为true。
So, do an easy check ; 因此,请进行简单检查;
If domain is not https and cookie_secure is set TRUE in config.php, cookies simply will not be set. 如果domain不是https,并且在config.php中将cookie_secure设置为TRUE,则将不会直接设置cookie。 Change to FALSE and it will be set in both http and https.
更改为FALSE,它将同时在http和https中设置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.