如何使用Symfony 2读取Cookie? [英] How to read a Cookie using Symfony 2?

查看:231
本文介绍了如何使用Symfony 2读取Cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我使用的公司建立一个网路应用程式的替代品。它是使用CakePHP 1.3构建的,我使用Symfony 2从头重写它。这两个系统并行运行,各种功能被转移到Symfony 2并从旧的CakePHP应用程序中删除。

I'm building a replacement for a web application the company I work for uses. It was built using CakePHP 1.3, and I'm rewriting it from the ground up using Symfony 2. The two systems will run side by side, with various features being transferred to Symfony 2 and removed from the old CakePHP application.

为了避免与登录细节混淆,我希望CakePHP处理用户登录的事情,直到我在一个点,我可以关闭CakePHP完全依赖Symfony 2安全。在此期间,CakePHP应用程序生成一个包含用户ID的cookie。我想将这个cookie用于Symfony作为一种基本的安全形式。

To avoid confusion with log in details, I want CakePHP to handle the user log in side of things until I'm at a point where I can turn off CakePHP altogether and rely on Symfony 2's security. In the meantime, the CakePHP application generates a cookie with contains a user ID. I want to use this cookie for Symfony as a rudimentary form of security.

但是,我无法获取Symfony读取或加载已创建的cookie。这是我的代码:

However, I'm unable to get Symfony to read or load the cookie that's been created. Here is my code:

public function securitytestAction()
{   

    $request = $this->getRequest($_COOKIE);
    $cookie = $request->headers->getCookies('CakeCookie[passport]');

    print_r($cookie);

    return $this->render('InstructorBundle:Default:test.html.twig');
}

使用Firebug,CakePHP在登录期间生成的Cookie命名为'CakeCookie [护照]'。

Using Firebug, the Cookie that CakePHP generates during the login is named 'CakeCookie[passport]'. It isn't encrypted either.

我得到的错误是:


FatalErrorException:Error:调用未定义的方法
Symfony \Component\HttpFoundation\HeaderBag :: getCookies()in
DefaultController.php line 713

FatalErrorException: Error: Call to undefined method Symfony\Component\HttpFoundation\HeaderBag::getCookies() in DefaultController.php line 713

我该怎么做才能让这项工作成功?

What could I do to get this working?

EDIT

Cheesemacfly建议更换:

EDIT
Cheesemacfly had suggested replacing:

$request = $this->getRequest($_COOKIE);
$cookie = $request->headers->getCookies();

只需:

$request = $this->getRequest()->cookies->all();

我得到以下回应:


Array([PHPSESSID] => gptbmh61mfkqn6p86d3suhnt13)

Array ( [PHPSESSID] => gptbmh61mfkqn6p86d3suhnt13 )

>

我一直在查看CakePHP用于生成Cookie的代码,并且没有为cookie提供域。我现在已经修复它,以便域 .domain.org ,以便它也可以访问子域。

EDIT 2
I've been looking at the code that CakePHP used to generate the Cookie, and it wasn't giving the domain for the cookie. I have now fixed it so that the domain is .domain.org, so that it can be accessed by subdomains as well.

现在,当我使用Firebug访问Symfony2应用程序时,我可以看到Cookie与其他两个CakePHP会话cookie(我不需要)一起列出。即使在查看子域 new.domain.org 时,3个CakePHP域都使用 www.domain.org 一个cookie Symfony2使用显示它的域名为 new.domain.org .Symfony2虽然仍然拒绝加载我想要的Cookie。

Now, when I go to the Symfony2 application using Firebug, I can see the Cookie listed along with the other two CakePHP session cookies (which I don't need). The 3 CakePHP domains all use www.domain.org even when viewing the subdomain new.domain.org, while the one cookie Symfony2 uses displays it's domain name as new.domain.org.Symfony2 though still refuses to load the Cookie I want.

推荐答案

我从朋友那里得到了帮助。

I figured it out with help to a friend.

基本上,在CakePHP中设置的cookie不包括路径。为了解决这个问题,我添加了以下PHP代码:

Essentially, the cookie that was being set in CakePHP didn't include a path. To combat this, I added the following PHP code:

$value = $someone['User']['id'];
setcookie("passport", $value, time()+1200, "/", ".domain.org");

然后我检查了Symfony2,而不更改上一次编辑的代码,然后Symfony2可以

I then checked Symfony2, without changing the code from the previous edit, and Symfony2 can then pick up the cookie.

感谢大家的帮助!

这篇关于如何使用Symfony 2读取Cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆