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

DNABeast's avatar

Access session variables outside of Laravel.

CKfinder grabs some config info from a .php file and I needed to be able to set a session variable to show I was logged in. Problem is that this file was called with javascript so didn't touch on Laravel. I needed a way to set a 'logged in' variable that the php SESSION could see.

Initially I would just set $_SESSION['IsAuthorized'] = true; when I logged in. Problem is that the session would log out if the browser closed but the user would still be authorised in Laravel. I finally pinged recently just how cool the middleware was. I simply changed the default Authenticate middleware by adding session variables whenever it checked.

public function handle($request, Closure $next)
    {
        session_start();
        if ($this->auth->guest())
        {
            $_SESSION['IsAuthorized'] = false; 
            if ($request->ajax())
            {
                return response('Unauthorized.', 401);
            }
            else
            {
                return redirect()->guest('login');
            }
        }
        $_SESSION['IsAuthorized'] = true; 
        return $next($request);
    }

Now to get access to the login I just need to start a session and call $_SESSION['IsAuthorized']

If I'm doing something silly please feel free to shoot me down.

0 likes
1 reply
Snapey's avatar

Not sure why this popped up, ITS FIVE YEARS AGO

Please or to participate in this conversation.