Here is my code to set cookie
$cookie = array(
'name' => 'City',
'value' => $city,
'expire' => 86400 * 30,
'domain' => '.localhost',
'path' => '/',
'prefix' => 'picker_',
);
$this->input->set_cookie($cookie)
and i loading cookie helper in constructor of a controller.
my cookie is set form 1 month and i check in browser my cookie is set name
'City' and value = '10'. the problem is when i retrieve the value i get the blank page nothing is printing
here the code of fetching value
echo $this->input->cookie('picker_City');
i also try var_dump
var_dump($this->input->cookie('City', false));
it return false
what is the error.
You have to load the helper cookie first before using set_cookie
$this->load->helper('cookie');
and then use set_cookie
If you are using cookies very often , its better you load it in the autoload.php in the config folder.
in the autoload array
$autoload['helper'] = array('cookie');
Did you load it?
NOTE:
For local testing, leave the domain value blank.
'domain' => '',
Related
I am using code igniter. I have to make cart feature and I have to save product ids to cookie but I am not able to add array to cookie. My controller code to add cookie is
public function add_to_cart(){
$product_id = $this->uri->segment(3);
$cookie= array(
'name' => 'cookie_product',
'value' => $product_id,
'expire' => '3600',
);
$this->input->set_cookie($cookie);
var_dump($this->input->cookie('cookie_product',true));die;
}
$product_id has my product id for example 41 . need help !
You do not have to use cookies directly. Use cart library which will handle cookies for you.
https://www.codeigniter.com/userguide3/libraries/cart.html
$this->load->library('cart');
$data = array(
'id' => 'sku_123ABC',
'qty' => 1,
'price' => 39.95,
'name' => 'T-Shirt',
'options' => array('Size' => 'L', 'Color' => 'Red')
);
$this->cart->insert($data);
And to view items
<?php print_r($this->cart->contents()); ?>
Cart sounds like a good solution.
The reason why your original code wasn't working is you were trying to pass an array instead of breaking it into individual parameters.
If you did want to fix it, your code should be:
$this->input->set_cookie($cookie['name'], $cookie['value'], $cookie['expire']);
Incidentally, you can store an array in a cookie by using serialize and unserialize to store the cookie as a string instead of an array:
$this->input->set_cookie('cookie_product', serialize($cookie));
var_dump(unserialize($this->input->cookie('cookie_product',true)));
I have a cookie that I set from domain A and trying to retrieve the value at domain B . Here is how I set my cookie from domain A.
$cookie = array(
'name' => 'userId',
'value' => $_SESSION['user']->id,
'expire' => time() + 3600,
'domain' => 'example.com',
'path' => '/',
'prefix' => 'mycookie_',
'secure' => FALSE,
);
$this->input->set_cookie($cookie);
So when I do var_dump($this->input->cookie('mycookie_userId')); it will return me the value like this string(2) "78"
From domain B I tried to print out the value var_dump(get_cookie('mycookie_userId')); but it return NULL.
I found the mistake. When setting the domain name I set it like example.com then I changed it to .example.com adding the dot in front then it working fine now.
This is my first post in stackoverflow and i'm really excited about that.
Actually i have problems with codeigniter cookie helper.
Take look at my codes:
$this->load->helper('cookie');
$cookie = array(
'name' => $nid,
'value' => 'checked'
);
$this->input->set_cookie($cookie);
echo $this->input->cookie();
// print_r($this->input->cookie();
as you see a declared an array called $cookie, then i set the cookie with that. but it seems that i made a mistake in setting cookies, because , the cookie doesn't set !
the immediate calling $this->input->cookie() returns nothing !
Does any one have any suggestion, where do i misunderstand the cookie helper of CI !?
var_dump() $this->input->set_cookie($cookie); // This returns NULL !
You need to give expiration time also in $cookie array and you are not echoing cookie what have you set. try this code
$this->load->helper('cookie');
$cookie = array(
'name' => $nid,
'value' => 'checked',
'expire' => '86500'
);
$this->input->set_cookie($cookie);
echo $this->input->cookie($nid, TRUE);
I am setting a cookie like so:
$cookie = array(
'name' => 'test',
'value' => 'test',
'expire' => 86500,
'domain' => '.iwantaspeaker.com',
'path' => '/'
//'secure' => TRUE
);
$this->input->set_cookie($cookie);
var_dump($this->input->cookie('test', false));
Which returns bool(false)
I get absolutely no response. I have in the config:
$config['cookie_prefix'] = "iwas_";
And the cookie is stored as iwas_test, so I have also tried $this->input->cookie("iwas_test",true); to no avail.
I can also see that the cookie is set in chrome:
Furthermore, I have also tried using the cookie helper. What must I do to retrieve the cookie correctly? the URL is on a local network machine, so the domain is pointed to the local IP with an entry in my hosts file, if this makes any difference.
ah-ha! make sure you dont pass expire as a string
$cookie = array(
'name' => 'test',
'value' => 'test',
'expire' => 86500, <--
'domain' => 'www.iwantaspeaker.com',
'path' => '/',
'secure' => TRUE <-- will only be set on https
);
// $this->ci->db->insert("UserCookies", array("CookieUserEmail"=>$userEmail, "CookieRandom"=>$randomString));
$this->input->set_cookie($cookie);
var_dump($this->input->cookie('iwas_test', false));
I am trying to use codeigniter cookies so that i can bookmark some topic , i don't know how to get cookies vale and use it to display bookmarked item in the user browser , it doesn't work when i try to display cokies value using print_r(); please help how to proceed. Thanks
function index(){
$this->load->helper('cookie');
$data['title'] = 'some bookmark';
$cookie = array(
'name' =>'mycookie',
'value' => 'some val',
'expire' => '86500',
'domain' => '.localhost',
'path' => '/',
'prefix' => 'something_'
);
set_cookie($cookie);
$this->load->view('bookmark', $data);
}
Browsers have trouble dealing with localhost (or .localhost) as the domain for cookie storage - they expect a domain to have at least two dots. Try setting the domain to FALSE (or don't set it) while testing.
If that still doesn't work, can you show us how you're retrieving the cookie data?