开发者

Why can't I set up my cookie duration with cakePHP

开发者 https://www.devze.com 2023-01-19 20:18 出处:网络
So, I\'ve tried many many things, but still always end up with Cookies that have the duration set to \"Session\" when looked at with Developers Tools in Google Chrome. Here are my current settings:

So, I've tried many many things, but still always end up with Cookies that have the duration set to "Session" when looked at with Developers Tools in Google Chrome. Here are my current settings:

core.php:

Configure::write('Session.cookie', 'session');
Configure::write('Session.timeout', '3600');
Configure::write('Session.start', true);
Configure::write('Security.level', 'high');

users_controller.php

$this->Cookie->write('xHi1PeWmAw', $user_record['User']['id']);

I tried changing the Security.level, the Session.timeout, using $this->Cookie->time = 3600; and combining all that, but I can't seem to be changing that duration. Also I tried with short and long durations, given that I would ideally fo开发者_Go百科r this cookie to last as long as possible. Can you please tell me what I am doing wrong?


I had the same problem (CakePHP 1.3.7) and now it works only if I put the duration within the write() method:

$this->Cookie->write ("cookie_name", "Some value", true, "+1 months");


Though I cannot guarantee this will fix your issue I can explain how to properly configure the sessions.

First you set your Security.timeout variable. This represents a timeout value in seconds, but this does not equal your sessions expiration time. This number is multiplied by a constant value depending on the setting of your Security.level variable.

'high' = x 10, 'medium' = x 100, 'low' = x 300,

This is what gives you your expiration time. For example, if you have a Session.timeout of 30, and a Security.level of low, your sessions will expire in 30*300 seconds, or 150 minutes.


If you you're using cookies as a session then it's time is set to 0. Which means it is set to expire when the browser closes. You could try to change this number in the controller as shown in the code below. See if that makes a difference. I have not tested this, but it is worth a shot.

var $components = array('Cookie');
function beforeFilter() {
   $this->Cookie->name = 'baker_id';
   $this->Cookie->time = 3600; // or '1 hour' //IF 0 THIS IS A SESSION COOKIE
   $this->Cookie->domain = 'example.com';
   $this->Cookie->secure = true; //i.e. only sent if using secure HTTPS
   $this->Cookie->key = 'qSI232qs*&sXOw!';
}
0

精彩评论

暂无评论...
验证码 换一张
取 消