How to work with CodeIgniter's cookie helper - codeigniter

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);

Related

Add array to cookie codeigniter

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)));

Not getting cookie value in codeigniter

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' => '',

Cookies not beeing set, codeigniter

I'm trying out cookies for first time in CI. Cant get it to work. Here's my code:
class Site extends CI_Controller {
public function index(){
$this->load->view('input_view');
$this->load->helper('cookie');
$this->test();
}
public function test(){
$cookie = array(
'name' => 'Test',
'value' => 'The Value',
'expire' => '86500',
//'domain' => '.some-domain.com',
'path' => '/',
'prefix' => 'myprefix_',
'secure' => TRUE
);
$this->input->set_cookie($cookie);
var_dump(get_cookie('Test'));
}
var_dump... returns false. I understand it does this the first time but should'nt it be true after reloading the page? I can't see any "Test" cookie in my web tools in Chrome either. Do I need the "domain" row? I googled some and if I understood it was optional? If it isn't, what am i suppose to write there? Localhost? In my config file i have this settings for cookies
$config['cookie_prefix'] = "";
$config['cookie_domain'] = "";
$config['cookie_path'] = "/";
$config['cookie_secure'] = FALSE;
I'm guessing you're not running https for development.
The secure flag on your cookie forces the cookie to be sent over HTTPS only;
Secure:
If this cookie can ONLY be sent over HTTPS (SSL), set this value to true.
Set it to false, and the cookie should work.

Should I save validation $_POST value into a variable or not in CI?

E.g. I have something like this in my controller:
$this->form_validation->set_rules('email', 'Email', 'required|valid_email|xss_clean');
$this->form_validation->set_rules('name', 'Name', 'trim|xss_clean');
Now my question is, if I later in my code do something like this (inside the same method):
$user_profiles = array(
'email' => $_POST['email'],
'name' => $_POST['name']
);
Will, the variables in this array sanitized or not?
I mean will the form_validation->set_rules remain for all the later code usage of the $_POST values or I need to use some other technique?
Codeigniter provides the input class which has a method post that can get the values you require, after they have been sanitised by the form validation:
$user_profiles = array(
'email' => $this->input->post('email'),
'name' => $this->input->post('name')
);
I would more or less recommend that you use $this->input->post() instead of $_POST unless there is a specific reason not to. The docs have some more explanation and uses of the input class: http://ellislab.com/codeigniter/user_guide/libraries/input.html.
I'm not 100% sure but as far as i know, form_validation just tests variables for given arguments, but dosen't clean them up.
I have always used $postdata = $this->input->post(NULL, TRUE); when handling form data.

how to get and use codeigniter cookies?

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?

Resources