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

Okay's avatar
Level 1

How to set a global session for the whole app?

Hello,

How can I set a global session in Laravel. In a normal PHP-script a global session like $_SESSION['myvar'] dies if the browser is closed or the function session_destroy() / unset() is called.

I tried this in Laravel, but it does not work in a controller extends BaseController:

Session::set( 'id', $user->id );
Session::get( 'id' );
0 likes
3 replies
EmilMoe's avatar

That's what a session is, information per session. You can store it in a file or the database or use environmental variables on the server.

Okay's avatar
Level 1

Example of my code:

// ROUTES
Route::get( '/', 'MyController@setSession' );


// CONTROLLER
class MyController extends Controller
{
    public function setSession( )
    {
       // ...
    Session::set( 'id', $user->id );
    return view('access');  
    }
}


// VIEW -> LOST Session
@if (Session::has('id') && Session::get('id') == 'hsdiwe78912hj')
    I have access!
@else
    I must stay here! <!-- I always get this -->
@endif 

Please or to participate in this conversation.