Symfony 1.4 cookie search fail

I am trying to store variables in cookies using the Symfony 1.4 framework. Here is a snippet from 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 went through the code in a debug session and I see that the cookie values ​​are being mapped in a variable sfWebResponse

, however no cookie is set - and this is demonstrated in the function preExecute()

- all the cookies received are null.

Why is this happening?

+3


source to share


2 answers


I made a simple test in a new symfony project.

I copied / pasted yours preExecute

and postExecute

and I 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 changed this line $path = '/products/';

to $path = '/';

.

On the first try, I have no cookies, so I have nothing:

array
  empty

      



And on refresh, the 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 might be wrong from your code is that you set the path to the cookie /products/

. This means that you can get access to this cookie only when you are on http://exemple.com/products/...

.

If you try to access these cookies on http://exemple.com/

, you will get nothing.

If that doesn't solve your problem, you can insert an example of the actions from which you have a problem with a specific uri (at least the part after the domain).

+3


source


When you pass the $ method path to the method, you can only use the cookie when accessing the cookie with the same url.

If you want to access the cookie from all over the site, don't miss anything along the way.



$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');

      

+1


source







All Articles