gpt4 book ai didi

php - Symfony2 : setting a cookie

转载 作者:可可西里 更新时间:2023-10-31 22:18:14 25 4
gpt4 key购买 nike

我正在尝试在登录 Controller 中设置一个 cookie 以实现“记住我”系统。即使我使用了我在网上找到的确切代码,但对我来说还是出错了。我希望你能帮我弄清楚我错过了什么。

让我们看一下代码:

public function loginAction(Request $request) {
// Receiving the login form
// Get Doctrine, Get EntityManager, Get Repository
if(/* form information matche database information */) {
// Creating a session => it's OK
// Creating the cookie
$response = new Response();
$response->headers->setCookie(new Cookie("user", $user));
$response->send();
$url = $this->generateUrl('home');
return $this->redirect($url);

} else
return $this->render('***Bundle:Default:Login.html.php');
}

我包括了这些:

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Cookie;

请注意,登录工作正常, session 已创建,但 cookie 尚未创建。

最佳答案

代替:

$response->send();

尝试使用:

$response->sendHeaders();

在此之后您应该能够重定向。

关于php - Symfony2 : setting a cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11567194/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com