Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Laravel5.4 $request->cookie('key') is empty when PHP global var $_COOKIE has cookies

I am trying to read the cookies in Laravel5.4. So I get the cookies after successful validation with third party Authorization server. I am not making the cookies in laravel5.4, but I am reading the cookie value send to me from view/frontend. I can see the values of cookies in Chrome debugger environment as well as from PHP global variable $_COOKIE. They are all set correctly.

However, when I tried to read the same cookie using $request->cookie('key') from controller method... it shows nothing. I also tried Cookie facade to read the value and still nothing. This controller is handling a redirect route which is called by the third party oauth server and this server when it redirects to my URL, it also sets the cookies.

Am I not setting this redirect url route correctly in web.php file in Laravel5.4? This is how it is defined in web.php as below:

Route::get('login/okta/callback', 'Auth\LoginController@handleProviderCallback');

The request object is empty when I dumped the content but $_COOKIE has the data.

like image 326
Andy Avatar asked Jan 30 '23 19:01

Andy


1 Answers

Laravel's cookie class encrypts and decrypts the values of its cookies (see the App\Http\Middleware\EncryptCookies middleware). When you have it look at a cookie it didn't set, it's going to try (and fail) to decrypt it.

If you've got a cookie set from outside Laravel, use $_COOKIE to work with it, or add the name of the cookie to the $except array in app/Http/Middleware/EncryptCookies.php so it isn't messed with.

like image 64
ceejayoz Avatar answered Feb 05 '23 16:02

ceejayoz