I'm having a cookie issue, the expiry date on my cookie is always being set to At End Of Session which isn't what I want. I did a bit of goggling and it suggested it set the expire to time()+60*60*24*30 which I've done.
//Create basket cookie
$cookie = array(
'name' => 'basket_id',
'value' => $basket_id,
'expire' => time()+60*60*24*30,
'domain' => 'domain',
'path' => '/',
'prefix' => '',
);
set_cookie($cookie);
I did wonder if it could be down to a Codeignter setting but my ci_session cookie has a normal expiry date. Thu, 09 Jun 2011 10:39:02 GMT
This is what I get when I view the cookie:
Name basket_id
Value 28
Host .host
Path /
Secure No
Expires At End Of Session
And here is an example of the array I'm passing to the cookie.
Array ( [name] => basket_id [value] => 30 [expire] => 1310202067 [domain] => host [path] => / [prefix] => )
Your expiry date is set incorrectly. You don't have to include the time(), as what you're setting is actually the expiry date from time().
When you have an incorrect expire value, it defaults to 0, which is set as your session's length instead.
Therefore it should be:
$cookie = array(
'name' => 'basket_id',
'value' => $basket_id,
'expire' => 86400*30,
'domain' => 'domain',
'path' => '/',
'prefix' => '',
);
Please check out the answer below by #Gowri for how to do it properly.
You can try to adjust session expiration time in config.php CI session initially is saved in cookies:
/** Session Variables
---------------------------------------
| 'session_expiration' = the number of SECONDS you want the session to last.
| by default sessions last 7200 seconds (two hours). Set to zero for no expiration.
|
*/
$config['sess_expiration'] = 7200;
You can add params
$config['cookie_lifetime'] = 1800
in config.php, the reason you can find in libraries/Sessions/Session.php, code below
$expiration = config_item('sess_expiration');
if (isset($params['cookie_lifetime']))
{
$params['cookie_lifetime'] = (int) $params['cookie_lifetime'];
}
else
{
$params['cookie_lifetime'] = (!isset($expiration) && config_item('sess_expire_on_close'))
? 0 : (int) $expiration;
}
Related
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.
I am using Auth Session in CakePHP.
'Session' => [
'timeout' => 0,
'defaults' => 'php',
'ini' => [
'session.cookie_secure' => false,
'session.cookie_lifetime' => 0
]
],
Already configured this in tha app.php but still getting logged out after few minutes.
Although Session persists when closing Browser. So i think its just a time related problem.
I was having requirement to increase session timeout in cakphp 2.5
the following code worked for me in 2.5 you can give this a try
//FILE: config/core.php
Configure::write('Session', array(
'defaults' => 'cake',
'cookie' => 'my_app',
'timeout' => 500, // 8 hours + 20 min, user will be logged in till 8 hours.
)
);
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' => '',
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 have a basket cookie, before adding a product to the basket the function checks to see if a cookie exists or if a new cookie is required. This works fine when adding one item at a time but there are occasions when more than one item is added at the same time. Again if an old basket cookie exists this works fine. The problem is when no basket cookie exists and the function had to create one.
The first time it goes through, the cookie is created a database record is added ect.
The second time round the function we check for the cookie no cookie is found and another cookie is created etc.
$this->db->select('basket_id');
$this->db->from($this->basket_table);
$this->db->where('basket_id', get_cookie('basket_id'));
$check_basket = $this->db->get();
if($check_basket->num_rows > 0){
$basket_exists = 1;
}else{
$basket_exists = 0;
}
if($basket_exists == 0){
delete_cookie('basket_id');
$basket = array(
'lang_id' => $lang_id,
'currency_id' => $currency_id,
'customer_id' => $customer_id,
);
$this->db->insert($this->basket_table, $basket);
$basket_id = $this->db->insert_id();;
$cookie = array(
'name' => 'basket_id',
'value' => $basket_id,
'expire' => 60*60*24*30,
'domain' => 'REMOVED'
'path' => '/',
'prefix' => '',
);
set_cookie($cookie);
}else{
$basket_id = get_cookie('basket_id');
}
In order to set the cookie, you need to send it to the browser, - but this never happens if your function loops multiple times before you create a view.
So set the cookie prior to them using the basket, OR only check if the cookie needs to be set once, like this:
$this->db->select('basket_id');
$this->db->from($this->basket_table);
$this->db->where('basket_id', get_cookie('basket_id'));
$check_basket = $this->db->get();
if($check_basket->num_rows > 0) {
$basket = array(
'lang_id' => $lang_id,
'currency_id' => $currency_id,
'customer_id' => $customer_id,
);
$this->db->insert($this->basket_table, $basket);
$basket_id = $this->db->insert_id();
$cookie = array(
'name' => 'basket_id',
'value' => $basket_id,
'expire' => 60*60*24*30,
'domain' => 'REMOVED'
'path' => '/',
'prefix' => '',
);
set_cookie($cookie);
}
// Now run your basket logic here - knowing the cookie is setup
}