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

Jacobs's avatar

Sorry, your session has expired. Please refresh and try again. 5.7

Hey, after updating my packages, going from 5.6.34 to 5.7.19, I keep getting this error when trying to log in, register, whatever form post with csrf.

Keeps throwing \Illuminate\Session\TokenMismatchException

Every form contains @csrf.

I've tried basically everything that people suggested when googling:

Deleted storage/framework/cache/data folder, made sure proper rights are set on storage/framework/cache.

Made sure everything is correct based on the 5.6 > 5.7 upgrade guide.

Made sure my form contains the csrf token, made sure my header also contains the token just in case (tried both @csrf & {{ csrf_field() }}).

Made sure my session.php contains 'domain' => env('SESSION_DOMAIN', null),.

Made sure my .env doesn't contain SESSION_DOMAIN.

Regenerated my APP_KEY.

Made sure my system date is correct.

Checked my SESSION_DRIVER is still file in .env.

Flushed caches via php artisan optimize:clear.

Dumped autoload via composer dump-autoload.

I.. think I'm on a dead end.. any suggestions?

0 likes
17 replies
Jacobs's avatar

Yeah, I've tried that too, new 5.7 doesn't work either.

Jacobs's avatar

@CRONIX -

ErrorException (E_NOTICE) unserialize(): Error at offset 0 of 40 bytes

Edit: Cleared cookies, still expired session issue.

D9705996's avatar

@JACOBS - It's really odd if your seeing this with a new app. What other features are you using like queues, etc... if you change these to file based do things work?

Reached's avatar

What's your environment like? Windows? Mac with Valet?

Jacobs's avatar

Don't think I have any special features that could affect that. Running on Ubuntu 18.04 with apache2 & mysql db

Jacobs's avatar

Funny thing is that when I disable VerifyCsrfToken middleware, I can't login either. There's no error message, nothing. I just don't get logged in. I can register, but I won't get logged in either.

zion's avatar

Can you check apache logs and see if there's any clue in there.

Jacobs's avatar

To add,

    public function login(Request $request)
    {
        $this->validateLogin($request);

        // If the class is using the ThrottlesLogins trait, we can automatically throttle
        // the login attempts for this application. We'll key this by the username and
        // the IP address of the client making these requests into this application.
        if ($this->hasTooManyLoginAttempts($request)) {
            $this->fireLockoutEvent($request);

            return $this->sendLockoutResponse($request);
        }

        if ($this->attemptLogin($request)) {
            return $this->sendLoginResponse($request);
        }

        // If the login attempt was unsuccessful we will increment the number of attempts
        // to login and redirect the user back to the login form. Of course, when this
        // user surpasses their maximum number of attempts they will get locked out.
        $this->incrementLoginAttempts($request);

        return $this->sendFailedLoginResponse($request);
    }

This doesn't get to sending failed login response, as ->attemptLogin($request) is successful.. yet user doesn't log in.. what the hell?

Jacobs's avatar

@ZION - error.log doesn't have anything popping up unfortunately

access.log as usual, nothing surprising

::1 - - [27/Dec/2018:14:42:21 +0100] "POST /login HTTP/1.1" 302 972 "http://thanatos.localhost/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"
realrandyallen's avatar

Are there any files in storage/framework/sessions? Maybe a permission issue on that folder?

Jacobs's avatar

@REALRANDYALLEN - Surprised as well, there are no files in there, though permissions are set, (as I've noted in the first post), using 777 doesn't help either.

Edit: drwxrwxrwx 2 www-data www-data 4096 dec 27 14:07 sessions

realrandyallen's avatar

@JACOBS - What's config.session.files set to?

Probably the default, storage_path('framework/sessions'), but covering all bases

Jacobs's avatar

Indeed default, 'files' => storage_path('framework/sessions'),

Jacobs's avatar
Jacobs
OP
Best Answer
Level 2

Okay.. I just found out the issue.

I had an ExtendedStartSession class which I've used to not count a certain route as previous.

Seems like something has changed during the upgrade and it broke, I'll investigate what broke and rewrite the code properly.

CSRF verification now works aswell, but I greatly appreciate your suggestions @realrandyallen as that reminded me that I've messed with that middleware a while ago.

Thus.. solved :)

Please or to participate in this conversation.