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

madala's avatar

TokenMismatchException

when i click logout in my application it shows TokenMismatchException i don't understand why? and also while i browse my application in middle it shows tokenmismatch exception. can any one help me with this.

0 likes
8 replies
maculus1's avatar

Hey MADALA, what is the value of the SESSION_DRIVER located in the .env file ?

Indemnity83's avatar

Madala; I'm guessing you're using a form for your logout button; any form will require that you include the CSRF Token; two ways to fix this kind of poblem

1 Add the csrf_token to the logout form

<form action="/logout">
    {{ csrf_field() }}   <!-- Add this line -->
    <button type="submit">Logout</button>
</form>

2 Use a basic link instead, no need for csrf token

<a href="/logout">Logout</a>
nikocraft's avatar

@Indemnity83 I haven't tried second approach but I am sure it will not work since laravel 5.3 logout action has been changed to post from get and you are giving example for get. I have also noticed sometimes that when I try to logout laravel throws up TokenMismatchException.

I think change from get to post for logout is not so smart in this case since users could be logged in to a website for hours and then when they try to logout it does not work.

nikocraft's avatar

This is by default the code Laravel generates and it's using POST

<a href="{{ url('/logout') }}"
    onclick="event.preventDefault();
             document.getElementById('logout-form').submit();">
    Logout
</a>

<form id="logout-form" action="{{ url('/logout') }}" method="POST" style="display: none;">
    {{ csrf_field() }}
</form>
nikocraft's avatar

@MADALA the only way to solve the problem is either increase the lifetime of session to a large value, currently it's 2 hours. Or use a package that automatically generates new token when old one expires.

Indemnity83's avatar

Ah you're right; the logout requires a post; but you said an interesting thing there...

the only way to solve the problem is either increase the lifetime of session to a large value

if your session times out; you are by definition already logged out; is it just the error message you're trying to avoid?

Please or to participate in this conversation.