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

fredmarks's avatar

My apps web Installer - session problems

I have created an application that comes with a web installer(e.g. like the Wordpress one).

  • The web installers asks for database details, etc
  • it then dynamically writes this information to the .env file.
  • The user then completes installation and they can log in and start using the app.

Here is the problem

Whilst testing, if I complete installing the application, login and use it for a while and then decide to do a new installation (same domain name, testing over and over), laravel is trying to connect to the database to authenticate the previous user (even though the .env file is clean and has no database information and has session driver set to 'file')

  • I have cleared all cache in laravel

If I clear my browser cache, the web installer works as expected, the user is able to set database information etc, which the installer then writes to the .env file and changes the session driver to database.

Even though I have cleared all the laravel cache files, what I think is happening is that the new installation is picking up on the browser cookie from the last time it was installed.

Log files show a timeout trying to connect to the database (which has not been set yet)

SQLSTATE[HY000] [2002] Connection timed out (SQL: select * from `users` where `id` = 1 limit 1) {"exception":"[object] (Illuminate\Database\QueryException(code: 2002): SQLSTATE[HY000] [2002] Connection timed out (SQL: select * from `users` where `id` = 1 limit 1) 

I hope I have explained it correctly

I have tried various things, including

//I have this in my web installers controller __construct()

$this->middleware('guest');

//inside my AppServiveProvider.php

//trying to delete any previous sessions, if installer has not completed
if (!env('SETUP_COMPLETED') ) {
       Auth::logout();
       \Session::flush();
}

none of this is working

Looking at the stack trace from the error log, I can see that it's the remember me cookie that the SessionGuard* is pulling up and trying to recreate the old session.

How can I fix this?

0 likes
9 replies
bobbybouwmann's avatar

So you should probably run the following things before you start a new installation

Artisan::class('cache:clear');
Artisan::class('config:clear');
Auth::logout();

This way everything should be reset. I think the reason this is happening is that your user is still marked as logged in and tries to retrieve the user on the new request. You need to make sure no sessions exist anymore. Auth::logout should be enough for that.

fredmarks's avatar

I have done all this, further more, I have physically deleted all the contents of the cache folder.

I have also added Auth::logout();

fredmarks's avatar

It certainly a cookie thing, because if I open a different browser, on the same computer, the installer starts just fine.

nikkinemo95's avatar

This Laravel package allows users who don't use Composer, SSH etc to install your application just by following the setup wizard, similar to how WordPress or any other CMS works.. It is very handy when you create a project that will be used by more than one client ( most of them don't know anything about Composer, SSH...) mcdvoice mybkexperience

fredmarks's avatar

Yes, the wizard I created will do the usual things like check for server requirements, folder permissions etc. I will then seed the database and update/write the .env file.

It's working very well, apart from this edge case scenario, where a user might want to reinstall on the same domain name than the run into this session issue

bobbybouwmann's avatar

You can try to manually delete the cookies before you go to that page. You can solve this in the same controller where you return the view or by a middleware as well. Remember that if you want to delete a cookie, it will only be deleted after your return a response.

Cookie::forget('laravel_session'); // Unless you changed the name of course
1 like
fredmarks's avatar

It looks like this is all happening while the app is bootstraping, so there is no way for me to remove the cookie before the check has been done. I have tried placing this code in all sorts of places, but the check will have already have been done.

I am trying to avoid changing core laravel files

fredmarks's avatar

I have created a Middleware called Setup and have placed it before the system middle ware.


//Kernel.php

    /**
     * The application's route middleware groups.
     *
     * @var array
     */
    protected $middlewareGroups = [
        'web' => [

            //setup
            \App\Http\Middleware\General\Setup::class,

            //system middleware
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            // \Illuminate\Session\Middleware\AuthenticateSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
            //[growcrm] [general middleware]
            \App\Http\Middleware\General\SanityCheck::class,
            //[growcrm] [general middleware]
            \App\Http\Middleware\General\General::class,
        ],

        'api' => [
            'throttle:60,1',
            'bindings',
        ],
    ];


//Setup middleware

    public function handle($request, Closure $next) {

        if (!env('SETUP_COMPLETED')) {
            Artisan::call('cache:clear');
            Artisan::call('route:clear');
            Artisan::call('config:clear');
            Session::flush();
            Cookie::forget('laravel_session');
            return $next($request);
        }

    }

Below is the stack trace

[2020-08-30 05:52:56] local.ERROR: SQLSTATE[HY000] [2002] Connection timed out (SQL: select * from `users` where `id` = 1 limit 1) {"exception":"[object] (Illuminate\Database\QueryException(code: 2002): SQLSTATE[HY000] [2002] Connection timed out (SQL: select * from `users` where `id` = 1 limit 1) at /var/www/html/mydomain.com/application/vendor/laravel/framework/src/Illuminate/Database/Connection.php:671)
[stacktrace]
#0 /var/www/html/mydomain.com/application/vendor/laravel/framework/src/Illuminate/Database/Connection.php(631): Illuminate\Database\Connection->runQueryCallback('select * from `...', Array, Object(Closure))
#1 /var/www/html/mydomain.com/application/vendor/laravel/framework/src/Illuminate/Database/Connection.php(339): Illuminate\Database\Connection->run('select * from `...', Array, Object(Closure))
#2 /var/www/html/mydomain.com/application/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(2202): Illuminate\Database\Connection->select('select * from `...', Array, true)
#3 /var/www/html/mydomain.com/application/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(2190): Illuminate\Database\Query\Builder->runSelect()
#4 /var/www/html/mydomain.com/application/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(2685): Illuminate\Database\Query\Builder->Illuminate\Database\Query\{closure}()
#5 /var/www/html/mydomain.com/application/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(2191): Illuminate\Database\Query\Builder->onceWithColumns(Array, Object(Closure))
#6 /var/www/html/mydomain.com/application/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(539): Illuminate\Database\Query\Builder->get(Array)
#7 /var/www/html/mydomain.com/application/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(523): Illuminate\Database\Eloquent\Builder->getModels(Array)
#8 /var/www/html/mydomain.com/application/vendor/laravel/framework/src/Illuminate/Database/Concerns/BuildsQueries.php(143): Illuminate\Database\Eloquent\Builder->get(Array)
#9 /var/www/html/mydomain.com/application/vendor/laravel/framework/src/Illuminate/Auth/EloquentUserProvider.php(68): Illuminate\Database\Eloquent\Builder->first()
#10 /var/www/html/mydomain.com/application/vendor/laravel/framework/src/Illuminate/Auth/SessionGuard.php(177): Illuminate\Auth\EloquentUserProvider->retrieveByToken('1', 'elMHZK0L2lQSBJ8...')
#11 /var/www/html/mydomain.com/application/vendor/laravel/framework/src/Illuminate/Auth/SessionGuard.php(147): Illuminate\Auth\SessionGuard->userFromRecaller(Object(Illuminate\Auth\Recaller))
#12 /var/www/html/mydomain.com/application/vendor/laravel/framework/src/Illuminate/Auth/GuardHelpers.php(60): Illuminate\Auth\SessionGuard->user()
#13 /var/www/html/mydomain.com/application/app/Http/Middleware/RedirectIfAuthenticated.php(20): Illuminate\Auth\SessionGuard->check()
#14 /var/www/html/mydomain.com/application/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): App\Http\Middleware\RedirectIfAuthenticated->handle(Object(Illuminate\Http\Request), Object(Closure))
#15 /var/www/html/mydomain.com/application/app/Http/Middleware/General/General.php(23): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#16 /var/www/html/mydomain.com/application/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): App\Http\Middleware\General\General->handle(Object(Illuminate\Http\Request), Object(Closure))
#17 /var/www/html/mydomain.com/application/app/Http/Middleware/General/SanityCheck.php(45): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#18 /var/www/html/mydomain.com/application/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): App\Http\Middleware\General\SanityCheck->handle(Object(Illuminate\Http\Request), Object(Closure))
#19 /var/www/html/mydomain.com/application/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php(41): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#20 /var/www/html/mydomain.com/application/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\Routing\Middleware\SubstituteBindings->handle(Object(Illuminate\Http\Request), Object(Closure))
#21 /var/www/html/mydomain.com/application/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php(76): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#22 /var/www/html/mydomain.com/application/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\Foundation\Http\Middleware\VerifyCsrfToken->handle(Object(Illuminate\Http\Request), Object(Closure))
#23 /var/www/html/mydomain.com/application/vendor/laravel/framework/src/Illuminate/View/Middleware/ShareErrorsFromSession.php(49): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#24 /var/www/html/mydomain.com/application/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\View\Middleware\ShareErrorsFromSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#25 /var/www/html/mydomain.com/application/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php(116): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#26 /var/www/html/mydomain.com/application/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php(62): Illuminate\Session\Middleware\StartSession->handleStatefulRequest(Object(Illuminate\Http\Request), Object(Illuminate\Session\Store), Object(Closure))
#27 /var/www/html/mydomain.com/application/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\Session\Middleware\StartSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#28 /var/www/html/mydomain.com/application/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/AddQueuedCookiesToResponse.php(37): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#29 /var/www/html/mydomain.com/application/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse->handle(Object(Illuminate\Http\Request), Object(Closure))
#30 /var/www/html/mydomain.com/application/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/EncryptCookies.php(66): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#31 /var/www/html/mydomain.com/application/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\Cookie\Middleware\EncryptCookies->handle(Object(Illuminate\Http\Request), Object(Closure))
#32 /var/www/html/mydomain.com/application/app/Http/Middleware/General/Setup.php(27): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#33 /var/www/html/mydomain.com/application/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): App\Http\Middleware\General\Setup->handle(Object(Illuminate\Http\Request), Object(Closure))
#34 /var/www/html/mydomain.com/application/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#35 /var/www/html/mydomain.com/application/vendor/laravel/framework/src/Illuminate/Routing/Router.php(687): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#36 /var/www/html/mydomain.com/application/vendor/laravel/framework/src/Illuminate/Routing/Router.php(662): Illuminate\Routing\Router->runRouteWithinStack(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request))
#37 /var/www/html/mydomain.com/application/vendor/laravel/framework/src/Illuminate/Routing/Router.php(628): Illuminate\Routing\Router->runRoute(Object(Illuminate\Http\Request), Object(Illuminate\Routing\Route))
#38 /var/www/html/mydomain.com/application/vendor/laravel/framework/src/Illuminate/Routing/Router.php(617): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
#39 /var/www/html/mydomain.com/application/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(165): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#40 /var/www/html/mydomain.com/application/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(128): Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}(Object(Illuminate\Http\Request))
#41 /var/www/html/mydomain.com/application/vendor/barryvdh/laravel-debugbar/src/Middleware/InjectDebugbar.php(58): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#42 /var/www/html/mydomain.com/application/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Barryvdh\Debugbar\Middleware\InjectDebugbar->handle(Object(Illuminate\Http\Request), Object(Closure))
#43 /var/www/html/mydomain.com/application/vendor/fideloper/proxy/src/TrustProxies.php(57): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#44 /var/www/html/mydomain.com/application/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Fideloper\Proxy\TrustProxies->handle(Object(Illuminate\Http\Request), Object(Closure))
#45 /var/www/html/mydomain.com/application/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#46 /var/www/html/mydomain.com/application/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Illuminate\Http\Request), Object(Closure))
#47 /var/www/html/mydomain.com/application/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#48 /var/www/html/mydomain.com/application/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Illuminate\Http\Request), Object(Closure))
#49 /var/www/html/mydomain.com/application/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php(27): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#50 /var/www/html/mydomain.com/application/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\Foundation\Http\Middleware\ValidatePostSize->handle(Object(Illuminate\Http\Request), Object(Closure))
#51 /var/www/html/mydomain.com/application/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php(63): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#52 /var/www/html/mydomain.com/application/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request), Object(Closure))
#53 /var/www/html/mydomain.com/application/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#54 /var/www/html/mydomain.com/application/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(140): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#55 /var/www/html/mydomain.com/application/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(109): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#56 /var/www/html/mydomain.com/index.php(69): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#57 {main}

The stack trace shows that

  • #32 /Http/Middleware/General/Setup.php is being executed before
  • #26 Illuminate/Session/Middleware/StartSession.php
fredmarks's avatar

Finally got this to work. I was making a really silly error.

In my Setup middleware, i was using

Auth::logout();

instead of

\Auth::logout();

because I had not specifed

use Auth;

Please or to participate in this conversation.