I am trying to store variables in cookies, using the Symfony 1.4 framework. Here is a snippet of my sfAction derived class:
class productActions extends sfActions
{
public function preExecute()
{
$this->no_registration_form = true;
$request = $this->getRequest();
$cookie_value = $request->getCookie('pcatid');
$this->prod_category_id = (!isset($cookie_value)) ? 0 : $cookie_value;
$cookie_value = $request->getCookie('pitid');
$this->product_item_id = (!isset($cookie_value)) ? 0 : $cookie_value;
$cookie_value = $request->getCookie('pcatnm');
$this->product_category_name = (!isset($cookie_value)) ? null : base64_decode ($cookie_value);
$cookie_value = $request->getCookie('pipnm');
$this->product_info_partial_name = (!isset($cookie_value)) ? null : $cookie_value;
$cookie_value = $request->getCookie('protl');
$this->title = (!isset($cookie_value)) ? null : base64_decode ($cookie_value);
}
public function postExecute()
{
$expire = time()+2592000;
$path = '/products/';
$response = $this->getResponse();
$value = $this->prod_category_id;
if (isset($value))
$response->setCookie('pcatid', $value, $expire, $path);
$value = $this->product_item_id;
if (isset($value))
$response->setCookie('pitid', $value, $expire, $path);
$value = base64_encode($this->product_category_name);
if (isset($value))
$response->setCookie('pcatnm', $value, $expire, $path);
$value = $this->product_info_partial_name;
if (isset($value))
$response->setCookie('pipnm', $value, $expire, $path);
$value = base64_encode($this->title);
if (isset($value))
$response->setCookie('protl', $value, $expire, $path);
}
public function executeIndex(sfWebRequest $request)
{
// uses defaults or settings stored in cookies
}
... // other functions that set the required instance member fields
}
I have stepped through the code in a debug session, and I can see that the cookie values are being collated in the sfWebResponse variable – however, no cookies are being set – and this is demonstrated in the preExecute() function – all the cookies retrieved have value null.
Why is this happening?
I’ve made a simple test on a fresh symfony project.
I’ve copy/pasted your
preExecute&postExecuteand I’ve added a simple action with an empty template:And I’ve changed this line
$path = '/products/';to$path = '/';.On the first try, I don’t have any cookies so I’ve nothing:
And on refresh, cookies were defined and I can see them:
What can be wrong from your code, is that you set the cookie path to
/products/. Which means, you’ll be able to access these cookie only when your are onhttp://exemple.com/products/....If you try to access these cookies on
http://exemple.com/, you’ll get nothing.If this doesn’t solve your problem, could you paste an example of an actions from where you got the problem with the defined uri (at least the part after the domain).