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

AbdulBazith's avatar

404 error usually occurs in my application, when back button clicked or long time inactivity

iam working with a online exam management project.

xyz.com is my application. i have a login form, students, and staff members will login and do their work. everything works fine. But iam facing a problem that, usually student face a 404 error| not found error.

iam not sure when students get these error. what i said to the students is to not clearing the cache, incorrect logout all these gives 404 error. but even they click the back button or refresh also they get this error

this is my route file


Auth::routes();

Route::get('logout', '\App\Http\Controllers\Auth\LoginController@logout');

Route::get('/', 'HomeController@index')->name('home');

this is my session.php

 'driver' => env('SESSION_DRIVER', 'file'),

  'lifetime' => 35791394,  

  'expire_on_close' => false,

  'encrypt' => false,

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

  'connection' => env('SESSION_CONNECTION', null),

 'table' => 'sessions',

    'store' => env('SESSION_STORE', null),
   

say for example xyz.com/ExamPage if for any reason they click back or something the url chage to

xyz.com/home and then it shows a 404 error|not found

student use the application in mobile, so they may not properly logout the application.

what my expectation is whatever the 404 error should not come.

this is homeController

<?php

namespace App\Http\Controllers;

use Session;

class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function index()
    {
        
        return view('dashboard');
    }
}

i dont know where i should change this, and what to do , even 419|page expired error also comes usually. all this is student login only. so i though the improper logout causes this error. is this right??

what i should do to stop this 404 error. kindly some one help

0 likes
11 replies
Snapey's avatar

Why are you so focussed on logout and sessions?

what url do they access?

You say they press back and go to xyz.com/home and get 404 but you don't say if /home is a valid Route?

AbdulBazith's avatar

@snapey thank you for your response

the student module have only 4 nav bars.

dashboard,   -- xyz.com

exam list,	-xyz.com/ExamList	

 exam summary,   -- xyz.com/ExamSummary

logout   - to logout the application

but i dont know how the url changes to xyz.com/home so do i need to write something for this?? then where i should write what changes i have to do?

but you don't say if /home is a valid Route? -> if this is the reason then in route what i should write

Route::get('/', 'HomeController@index')->name('home'); this is the reason for that error??

Snapey's avatar

You don't understand what are valid routes in your application? ??

So sometimes the user is sent to /home but you don't have such a route?

AbdulBazith's avatar

@snapey yes iam not clear about valid routes. kindly guide me whats the mistake what i should do now?

till now i get confused that how it changing xyz.com/home

kindly point out whats wrong with me in this portion

Snapey's avatar

/home is probably in one of your middlewares

What Laravel version is it?

AbdulBazith's avatar

@snapey @sinnbeck

my laravel version is - Laravel Framework 6.18.0

and app\Providers\RouteServiceProvider.php file

<?php

namespace App\Providers;

use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Route;

class RouteServiceProvider extends ServiceProvider
{
    /**
     * This namespace is applied to your controller routes.
     *
     * In addition, it is set as the URL generator's root namespace.
     *
     * @var string
     */
    protected $namespace = 'App\Http\Controllers';

    /**
     * The path to the "home" route for your application.
     *
     * @var string
     */
    public const HOME = '/home';

    /**
     * Define your route model bindings, pattern filters, etc.
     *
     * @return void
     */
    public function boot()
    {
        //

        parent::boot();
    }

    /**
     * Define the routes for the application.
     *
     * @return void
     */
    public function map()
    {
        $this->mapApiRoutes();

        $this->mapWebRoutes();

        //
    }

    /**
     * Define the "web" routes for the application.
     *
     * These routes all receive session state, CSRF protection, etc.
     *
     * @return void
     */
    protected function mapWebRoutes()
    {
        Route::middleware('web')
             ->namespace($this->namespace)
             ->group(base_path('routes/web.php'));
    }

    /**
     * Define the "api" routes for the application.
     *
     * These routes are typically stateless.
     *
     * @return void
     */
    protected function mapApiRoutes()
    {
        Route::prefix('api')
             ->middleware('api')
             ->namespace($this->namespace)
             ->group(base_path('routes/api.php'));
    }
}

So what should i do now??

Sinnbeck's avatar

Try checking app\Providers\RouteServiceProvider.php. It probably has

public const HOME = '/home';
Sinnbeck's avatar

Change the HOME to a route that exists and makes sense for your app :)

For instance

  /**
     * The path to the "home" route for your application.
     *
     * @var string
     */
    public const HOME = '/';
Snapey's avatar

change /home in that file to a valid 'home' page for the user to go to

I think the route is called when the user is logged in via the 'remember me' functionality.

So if the user has been away for a while and returns, they are not prompted to login just sent HOME

BUT you need to find a reliable way to produce this error so that you know you have fixed it

AbdulBazith's avatar

@snapey @sinnbeck thank you guys.

So if the user has been away for a while and returns, they are not prompted to login just sent HOME

yes yes the above line was exactly happening. actually student may write the exam for 2 hrs or 4 hrs,

so what my client expect to not logout after several minutes so in session i changed it as

'lifetime' => 35791394,

so currently i will change the public const HOME = '/home'; this to public const HOME = '/';

But is this the permanent solution?? and even after 1 or 2 hrs students clicks logout they face 419|page Expired Error to avoid this error only i have increased the life time in session but still this error persist why??

BUT you need to find a reliable way to produce this error so that you know you have fixed it ->so what should i do @snapey

Snapey's avatar

This

But is this the permanent solution?? and even after 1 or 2 hrs students clicks logout they face 419|page Expired Error to avoid this error only i have increased the life time in session but still this error persist why??

is discussed here; https://talltips.novate.co.uk/laravel/csrf-and-expired-logout-forms

Check session lifetime with tinker

>>> config('session')

to make sure your incredibly long lifetime is taken correct

Please or to participate in this conversation.