Search code examples
phpcookiessymfony1symfony-1.4

Symfony 1.4 cookie retrieval failure


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?


Solution

  • 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).