Laravel 5.3: No Cookies Are Set On Browser, Getting TokenMismatchException

- 1 answer

I have a Laravel 5.3 app on Ubuntu 18.04. Whenever I submit a form I get the following error

TokenMismatchException in VerifyCsrfToken.php

I checked, my form token is present in the form

<input type="hidden" name="_token" value="1axYN6BB8nkhoncOrLshJykbAhmlsjoxiDvYYqxI">

I checked my cookies, no cookies are being created on client side. In chrome development tool Storage/Cookies, no values present.

So I think the issue is cookies are not set on my browser

My server storage permission is 777 and I can files inside storage/framework/sessions folder.

The same applications work on my localhost (windows)

What could be the reason for no cookies?



I had a similar problem in Laravel 5.3. It is solved in later versions, but I have several apps that also remain on 5.3 due to breaking changes that we can't update in prod.

There are several reasons why this might be happening. Here are a few that may help you:

1) You didn't say how you got the value on your form. Make sure the value is supplied by Laravel: ie value="{{ csrf_token() }}"

2) Check for spaces in your routes/web.php file. In this version of Laravel, a space before the opening tag <?php caused it to fail.

3) You say this was working on one server and not another. If one is using a non-secure connection with SESSION_SECURE_COOKIE set to true, this could be the issue. Try flipping to false and test.

4) This was my solution: In config/session.php set 'domain' => env('SESSION_DOMAIN', null), or your site, and then either remove SESSION_DOMAIN from the file (.env), or set it to your site. Then run composer dumpautoload, and clear caches. I had the wrong session setup.

Hopefully one of these will point you in the right direction.