i tried following code for set cookie but i can't get the cookie.
if($this->input->post('remember')){
$this->load->helper('cookie');
$cookie = array(
'name' => 'remember_me',
'value' => 'test',
'expire' => '300',
'secure' => TRUE
);
set_cookie($cookie);
}
and following code is for get cookie
$cookie= get_cookie('remember_me');
var_dump($cookie);
can any one tell me what's the problem is there? thanks in advance.
Use
$this->input->set_cookie($cookie);
instead of set_cookie($cookie);
You need to create a controller class and add the following code to it;
<?php
if ( ! defined('BASEPATH')) exit('Stop Its demostrate how to set cookie');
class cw_cookies extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper('cookie');
}
function set()
{
$cookie= array(
'name' => 'remember_me',
'value' => 'test',
'expire' => '300',
'secure' => TRUE
);
$this->input->set_cookie($cookie);
echo "Congratulation Cookie Set";
}
function get()
{
echo $this->input->cookie('remember_me',true);
}
}
The above code sets the cookies through
$this->input->set_cookie()
The helper is loaded using:
$this->load->helper('cookie');
You can read more at : Set Cookies in Codeigniter
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With