Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a global session for the whole app in Laravel?

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' );

The Context of my code is:

// 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 
like image 283
Okay Avatar asked Oct 28 '25 23:10

Okay


1 Answers

use

Session::put('id', $user->id );
Session::get('id' );
Session::forget('id' );
like image 91
MD. ABU TALHA Avatar answered Oct 31 '25 12:10

MD. ABU TALHA