簡體   English   中英

Symfony2:設置cookie

[英]Symfony2: setting a cookie

我正在嘗試在登錄控制器中設置一個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;

請注意,登錄工作正常,會話已創建,但cookie尚未創建。

代替:

$response->send();

嘗試使用:

$response->sendHeaders();

在此之后你應該能夠重定向。

默認情況下, Symfony \\ Component \\ HttpFoundation \\ Cookie創建為HttpOnly ,它會觸發支持瀏覽器的安全措施; 這有助於減輕javascript中可能發生的某些XSS攻擊。

要在這樣的瀏覽器中公開cookie,請將$httpOnly參數設置為false

new Cookie('user', $user, 0, '/', null, false, false); //last argument

值得注意的是,在編輯時,框架被配置為默認情況下使用HttpOnly cookie:請參閱cookbook(cookie_httponly)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM