php - Symfony 1.4 cookie 检索失败

标签 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
}

我已经在调试 session 中逐步执行代码,我可以看到 cookie 值正在 sfWebResponse 变量中进行整理 - 但是,没有设置任何 cookie - 这在preExecute() 函数 - 所有检索到的 cookie 的值为 null。

为什么会这样?

最佳答案

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

我已经复制/粘贴了您的preExecutepostExecute,并且添加了一个带有空模板的简单操作:

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 = '/';

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

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(至少是域之后的部分)出现问题的地方。

关于php - Symfony 1.4 cookie 检索失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14767258/

相关文章:

javascript - 从 Google Chrome 扩展访问 cookie

cookies - 如果有来自 xmlhttprequest 的 set-cookie 响应,浏览器是否会接受它并设置 cookie?

c# - 如何在我的 ASP.net Core 应用程序中同时使用 Bearer 和 Cookie 身份验证?

php - 检查哪些用户在线

symfony - 在 symfony 中查找当前 Doctrine 数据库连接设置

full-text-search - 使用 Lucene 通过 Symfony 为 PDF 文件建立索引

symfony1 - Symfony 1.3 与 Propel 1.5 : Memory issues

php - php for 循环内的 HTML 标签不采用或添加动态 css 和 div 标签

javascript - 指定请求桌面站点位置

php - 在多个托管 XML 文件中搜索字符串