Laravel 5.4 cookie value

I am trying to get a cookie value.

1 When I use the Laravel request cookie helper:

$request->cookie('CookieName'); 

Laravel returns the name of the cookie instead of its value.

2 When I dd () the cookie () function:

 dd(cookie('CookieName')); 

I get:

 #name: "CookieName" #value: null #domain: null #expire: 0 #path: "/" #secure: false #httpOnly: true -raw: false -sameSite: null 

3 When I use the PHP assembly in the $ _COOKIE function:

 $_COOKIE['CookieName']; 

I really get the cookie value.


Is there a way to get Lavavel to return a cookie value?

+5
source share
2 answers

The correct way to get the cookie value will be as you used

 $request->cookie('name'); 

But the helper cookie method creates a new cookie, not a value. Therefore, when you do dd(cookie('CookieName')); , it creates a cookie with this name and does not matter and returns it.

Laravel encrypts and decrypts the cookie value on the fly without user intervention. Check how you configure cooking again, and also make sure you set APP_KEY , which will be used for encryption. Changing this key will invalidate all old cookies.

+3
source

Actually

 $value = $request->cookie('name'); 

should indicate a value that you can read in the doc .

I suspect that your cookie is configured from some external code (not laravel code), for example, it is created by the jQuery plugin or something else. In this situation, you should add your cookie to the EncryptCookies $ middleware with the exception of the table. Since all cookies created within Laravel are encrypted and signed with an authentication code. All other cookies, for example, from the jQuery plugin, are not encrypted or signed by Laravel, so $ request-> cookie ('name') cannot see them or their value.

+2
source

Source: https://habr.com/ru/post/1268067/


All Articles