Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

assadullahch's avatar

Laravel 5.4 is not creating csrf cookie on my hosting

Hello. I have developed a website in Laravel 5.4. It works fine on localhost but when i uploaded on hosting server, it throws this exception

TokenMismatchException in VerifyCsrfToken.php line 68:

I did some digging and I figured out Laravel is unable to set csrf cookie on my server.

Please help me out to fix this.

Thanks

1 like
24 replies
jlrdw's avatar

Laravel or hosting has nothing to do with it you need to double check all of your code you have made a mistake somewhere.

Snapey's avatar

it's not a cookie, it's a token that you can see in the html source.

check the source passed to the browser and see if the token is present

jlrdw's avatar

Also the way it works is a stored token in session is supposed to match a token passed via post, are you sure you have your storage folder permissions correct?

Edit: If you get your sessions working, all will be okay.

assadullahch's avatar

@Snapey Laravel stores csrf token in a session and the corresponding cookie to that session is stored in the browser by the name CSRF-TOKEN. My website is unable to set any type of cookies. In the source code the csrf_field shows the token in the form which is OK. But cookie related to csrf session does not gets created and that results in TokenMismatchException in VerifyCsrfToken.php line 68:

assadullahch's avatar

@jlrdw i have changed storage folder permissions to 777 from 755 but it still does not work. Locally it works perfectly fine.

Snapey's avatar

it's the token in your form that is compared to the value stored in session on the server.

assadullahch's avatar

@Snapey I know the token is compared with the token stored in the session. What I am saying is, session identifiers are automatically stored in the corresponding cookies and in my case i can't see CSRF-TOKEN cookie in my browser. CSRF-TOKEN gets created when I run my script on localhost only and this is the reason Laravel throwing this exception TokenMismatchException in VerifyCsrfToken.php line 68:

jlrdw's avatar

Have you checked your config/session

assadullahch's avatar

I am using default session settings which comes up with fresh installation. Sessions are getting created but not the corresponding cookies. This is the main issue. Because cookies are sent with every HTTP request and then the session identifier is compared with the cookie sent automatically. In my case there are no cookies, so the user cannot be considered as an authenticated user.

jlrdw's avatar

Sessions and cookies are two different things.

assadullahch's avatar

I know they are two different things but they have a link. How would a session gets validated if it's corresponding cookie (which holds it's id and sent with every HTTP Request) does not exist in the browser? In my case, there are sessions are created but not the corresponding cookies.

tisuchi's avatar

You need to ensure either 1 in your html source code.

<meta name="csrf-token" content="a8YwpsXSj6XZI2Re28NturuU3BlyG5YZVGf8042h">

OR

<input type="hidden" name="_token" value="a8YwpsXSj6XZI2Re28NturuU3BlyG5YZVGf8042h">

In html form as a hidden value.

Just go to your page, and check html source code that whether there is csrf token or not.

assadullahch's avatar

@tisuchi it's already there mate. The only issue is, my website is unable to create cookies through laravel.

tisuchi's avatar

So far, its not issue with cookies. Its just rendering to html templates.

You may be check few things here-

  • Check in other browser
  • Check in other computer

If everything is ok there, than, delete your browser history and run it again in your computer.

It should work than.

assadullahch's avatar

@tisuchi I don't think it has issue with html template since i checked token gets submitted with the form submit but the hash stored in the session does not match with it. The reason is there is no cookie that can help Laravel to search for the right session.

assadullahch's avatar

I was right. The website was unable to create cookies. The Laravel was not setting up CSRF-COOKIE and other cookies for don't know what reason. Now I uploaded the same files one by one and it works. It's really a strange behavior.

dharmendrajadon's avatar

Inside config directory, edit session.php and check these lines:

'cookie' => 'cookie_name',
'path' => '/',
'domain' => env('SESSION_DOMAIN', null),
'secure' => env('SESSION_SECURE_COOKIE', false),
'http_only' => true,
// And modify domain value inside .env to null or as your domain
// Like: null or example.com
assadullahch's avatar

@dharmendrajadon I managed to make it work the day i posted my question here. There was nothing wrong with the configuration settings. When I uploaded the whole project at once, It just stuck at csrf mismatch exception but the same project works fine when i uploaded files one by one. It was pretty strange though.

ycherif's avatar

Verify that your config/session.php file contains this line

'domain' => env('SESSION_DOMAIN', null),

Then remove the SESSION_DOMAIN line in your .env file

patpaskoch's avatar

Hi all, I think I have the same problem, I uploaded my project from my local to a https production server. On my local site everything works fine but on my production server, when I check the cookies on my landing page there is no CSRF-Token but on my local its getting created. I also give log out the token after my submission and they don't fit.

Anyone any idea?

npubet's avatar

Laravel 9 + Chrome (localhost or with IP address). CSRF token mismatch error and no CSRF in cookies.

My solution:

SESSION_SECURE_COOKIE=false (.env file)
'same_site' => 'lax' (session.php file)

Please or to participate in this conversation.