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

hussain_nayani's avatar

419 - Session Expired without event authentication after deployment | Laravel 5.7

My Laravel application works fine in on my local machine and on my staging server. But when I deployed to my production server the login form is not working. Every time I try to login to the admin panel it shows my

419

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

My Head Tag contains <meta name="csrf-token" content="XXX">

My login form contains <input type="hidden" name="_token" value="XXX">

What have I tried so far:

  • Tried generating new key php artisan key:generate
  • Tried clearing all caches
  • php artisan cache:clear
  • php artisan route:clear
  • php artisan view:clear
  • php artisan cache:clear
  • Checked php.ini max_execution_time and memory limits.
  • Tried changing SESSION_DRIVER from file to database

The only thing that kind of worked but was unsafe and was for debugging purpose

In the VerifyCsrfToken class, I added "api/login" & "api/register" in protected $except and the form worked. Like this


namespace FleetCart\Http\Middleware;

use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as BaseVerifier;

class VerifyCsrfToken extends BaseVerifier
{
    /**
     * The URIs that should be excluded from CSRF verification.
     *
     * @var array
     */
    protected $except = [
        "api/login",
        "api/register"
    ];
}

Please help me understand how to resolve this.

Thanks in advance.

0 likes
13 replies
abhijeet9920's avatar

Hello @hussain_nayani ,

From your explanation, I'm assuming that you've created two web services for signing a user and registering a user. Laravel has a built-in mechanism for CSRF protection. In all POST requests, additional csrf_token is expected as a parameter, and this parameter is being checked on serverside. You can generate csrf token using

csrf_token();//helper
//or
//
<form method="POST" action="/profile">
    @csrf
    ...
</form>

Please go through documentation.

You can disable this csrf protection by specifying your routes as you've done

<?php
namespace App\Http\Middleware;

use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as BaseVerifier;

class VerifyCsrfToken extends BaseVerifier
{
    /**
     * The URIs that should be excluded from CSRF verification.
     *
     * @var array
     */
    protected $except = [
        "api/login",
        "api/register"
    ];
}

However, this may not be good practice, since in the future you may need to work on API with either GET or POST or whatever methods.

Another simple solution is to use api.php file for your API routes. It's already there in routes directory from 5.3. If you go to Kernal.php, you'll see two middleware groups.web middleware group is applicable for your web routes, i.e. routes you've added in web.php file, and api group is applicable for routes added in api.php file. A class VerifyCsrfToken is being called only for web routes.

<?php

namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel
{

    ...
    protected $middlewareGroups = [
        'web' => [
            ...
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],
        'api' => [
            ...,
        ],
    ];
    ...
}

I would suggest you add these two routes in api.php.

Apology for the long explanation.

hussain_nayani's avatar

Thank-you so much @abhijeet9920 for your response to the matter and your long explanation is much appreciated. However I regret to tell you that this didn't worked.

I am not getting the session expired error now but still unable to get logged in.

The Auth::user() still returns null

PS: I am sorry for my dumb questions/responses. I am new to Laraval.

abhijeet9920's avatar

Even a dumb question makes a valid point, so no problem. It will be really helpful if you give a sample code. If I'm not wrong, you're working on API authentication.

You can visit here. This will be really helpful to you.

hussain_nayani's avatar

Thanks @abhijeet9920 for being so positive.

No, I am not using API authenticate, instead I am working an a web application. Here's the code of the login form. Please let me know if you want me to share any other code snippets form my app.

<form method="POST" action="{{ route('login.post') }}" class="login-form clearfix">
                {{ csrf_field() }}

                <div class="bg-blue">
                    <div class="reflection"></div>
                </div>

                <div class="login form-inner clearfix">
                    <a href="{{ route('register') }}" class="register" data-toggle="tooltip" data-placement="top" title="{{ trans('user::auth.register') }}" rel="tooltip">
                        <i class="fa fa-user-plus" aria-hidden="true"></i>
                    </a>

                    <h3>{{ trans('user::auth.login') }}</h3>

                    <div class="form-group {{ $errors->has('email') ? 'has-error': '' }}">
                        <label for="email">{{ trans('user::auth.email') }}<span>*</span></label>

                        <input type="text" name="email" value="{{ old('email') }}" class="form-control" id="email" placeholder="{{ trans('user::attributes.users.email') }}" autofocus>

                        <div class="input-icon">
                            <i class="fa fa-envelope-o" aria-hidden="true"></i>
                        </div>

                        {!! $errors->first('email', '<span class="error-message">:message</span>') !!}
                    </div>

                    <div class="form-group {{ $errors->has('password') ? 'has-error': '' }}">
                        <label for="password">{{ trans('user::auth.password') }}<span>*</span></label>

                        <input type="password" name="password" class="form-control" id="password" placeholder="{{ trans('user::attributes.users.password') }}">

                        <div class="input-icon">
                            <i class="fa fa-lock" aria-hidden="true"></i>
                        </div>

                        {!! $errors->first('password', '<span class="error-message">:message</span>') !!}
                    </div>

                    <div class="clearfix"></div>

                    <button type="submit" class="btn btn-primary btn-center btn-login" data-loading>
                        {{ trans('user::auth.login') }}
                    </button>

                    <div class="checkbox pull-left">
                        <input type="hidden" value="0">
                        <input type="checkbox" value="1" id="remember">

                        <label for="remember">{{ trans('user::auth.remember_me') }}</label>
                    </div>

                    <a href="{{ route('reset') }}" class="forgot-password pull-right">
                        {{ trans('user::auth.forgot_password') }}
                    </a>
                </div>
            </form>
hussain_nayani's avatar

Hello @snapey thank-you for you response.

I used this command to find all the files with the blank first line and found some view files find . -name '*php' -exec awk 'NR==1&&/^$/{print FILENAME}' {} \;

Unfortunately it didn't helped.

Snapey's avatar

as I said in that post, view files will not be an issue

and its not just blank lines. Its ANY character before the <?php

hussain_nayani's avatar

Thank-you so much @snapey

After spending hours I found a file that have a space before starting PHP tag.

Thank you much sir. You saved my life.

juanfecode's avatar

@snapey Thank you, but I did not find any files with this problem.

Another page they suggested to disable @csrf with this code. I added it to the VerifiCsrfToke.class and it worked.

I still don't understand the cause of this error?

 public function handle($request, \Closure $next)
    {
        if (in_array(env('APP_ENV'), ['local', 'dev'])) {
            return $next($request);
        }

        return parent::handle($request, $next);
    }
hussain_nayani's avatar

Hi @juanfecode , I found a file in my project which was having extra space before starting PHP tag. I removed it and it started working fine.

zoxed's avatar

which file was that if you may please, and also i have have a problem whenever i trie to log in to the admin panel i get this error " 500 | server error" and i reload the page and it worked but seeing it every time ...

Please or to participate in this conversation.