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

leo4all's avatar

Session Issue in Middleware

Session Issue inside Middleware

I can't get the previous session, why its always the same as the $request->getHost();

public function handle($request, Closure $next)
    {
        $parts = explode('.', $request->getHost());
        $subdomain = $parts[0];
        
        /*$domain = DomainList::where('subdomain', '=', $subdomain)->first();
        if($domain == null) {
            abort(404);
        }*/

        $res = \DB::select("SHOW DATABASES LIKE '" . $subdomain . "'");
        if(count($res) == 0) {
            abort(404);
        }
        
        if(!\Session::has('subdomain')) {
            \Session::put('subdomain', config('database.connections.mysql.database'));
        }

        if($subdomain <> \Session::get('subdomain')) // ISSUE HERE
        {
            config(['database.connections.mysql.database'=> $subdomain]);
            \DB::reconnect();
        }

        \Session::put('subdomain', $subdomain); 
        \Session::save();
       

        return $next($request);
    }
0 likes
0 replies

Please or to participate in this conversation.