Symfony 1.4 cookie检索失败

时间:2013-02-08 06:53:31

标签: php cookies symfony1 symfony-1.4

我正在尝试使用Symfony 1.4框架将变量存储在cookie中。这是我的sfAction派生类的片段:

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
}

我已经在调试会话中逐步完成了代码,我可以看到cookie值正在sfWebResponse变量中进行整理 - 但是,没有设置cookie - 这在{{ 1}} function - 检索到的所有cookie都为null。

为什么会这样?

2 个答案:

答案 0 :(得分:4)

我对一个新的symfony项目做了一个简单的测试。

我已复制/粘贴了您的preExecute& postExecute我添加了一个带有空模板的简单操作:

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;
}

我已将此行$path = '/products/';更改为$path = '/';

第一次尝试时,我没有任何饼干所以我什么都没有:

array
  empty

刷新时,Cookie已定义,我可以看到它们:

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)
您的代码中的

可能有什么问题,就是您将Cookie路径设置为/products/。这意味着,当您使用http://exemple.com/products/...时,您将能够访问这些Cookie

如果您尝试在http://exemple.com/上访问这些Cookie,您将什么也得不到。

如果这不能解决您的问题,您是否可以粘贴一个动作示例来解决您所定义的uri问题(至少是域之后的部分)。

答案 1 :(得分:1)

当您将$ path传递给该方法时,您只能在从同一网址访问Cookie时使用该Cookie。

如果您想从所有网站访问Cookie,请不要在路径中传递任何内容。

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