4
votes

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?

2
Are the cookies set when you check in browser?Michal Trojanowski
@MichalTrojanowski: Yes the cookies are set when I check the brower (FF). The problem appears therefore, to lie with the retrieval code. I will ammend the title to reflect this.Homunculus Reticulli
Does var_dump($_COOKIE) inside the preExecute show your cookies?j0k
@j0k: var_dump($_COOKIE) inside preExecute() shows the existing cookies, but crucially, not the ones I am setting in my postExecute.Homunculus Reticulli
Can we see an action exemple? Cookies are setted when the sfWebResponse send header. So I'm wondering if the tested action send the header properly (I mean, using a normal layout, etc ..)j0k

2 Answers

4
votes

I've made a simple test on a fresh symfony project.

I've copy/pasted your preExecute & postExecute and I've added a simple action with an empty template:

public function executeIndex(sfWebRequest $request)
{
  var_dump($_COOKIE);

  $this->prod_category_id          = 123;
  $this->product_item_id           = 456;
  $this->product_category_name     = 789;
  $this->product_info_partial_name = 159;
  $this->title                     = 753;
}

And I've changed this line $path = '/products/'; to $path = '/';.

On the first try, I don't have any cookies so I've nothing:

array
  empty

And on refresh, cookies were defined and I can see them:

array
  'symfony' => string 'c04khrspp5fmg3sh797uq9c9s1' (length=26)
  'pcatid' => string '123' (length=3)
  'pitid' => string '456' (length=3)
  'pcatnm' => string 'Nzg5' (length=4)
  'pipnm' => string '159' (length=3)
  'protl' => string 'NzUz' (length=4)

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 on http://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).

1
votes

When you pass the $path to the method you can only use the cookie when accessing the cookie from the same URL.

If you want to access a cookie from all the site, don't pass anything in the path.

$response = $this->getResponse();
$response->setCookie('stack', 'This cookie is readable from all site for the next 60 segs.', time() + 60);
$response->setCookie('overflow', 'This cookie is readable from mypage for the next 60 segs.', time() + 60, 'http://mysite.com/mypage');