21

I am trying to remove a basic session but it's not removing. Here's the code

welcome.blade.php

@if(Session::has('key'))             
    {{ Session::get('key')}}
    <a href="logout">Sign Out</a>

    @else
        please signin

    @endif
</div>

I don't know how to remove session. Here's the one I used but it's not working route.php

Route::get('/logout', function() {

   $vv =  Session::forget('key');
   if($vv)
   {
        return "signout";
   }
});
Dexter Bengil
  • 5,995
  • 6
  • 35
  • 54
rockie
  • 227
  • 1
  • 2
  • 8
  • You should really use Laravel's built in authentification: http://laravel.com/docs/5.0/authentication – lukasgeiter Apr 05 '15 at 21:32
  • can you show me a example for login and logout – rockie Apr 05 '15 at 22:05
  • First read the documentation I linked, second search online for other resources (tutorials, SO questions, etc) and if you still haven't found anything, ask me again. As developer one of the most important skills is to do research and know where to find information. Especially with such basic questions it is pretty easy to find something online. – lukasgeiter Apr 05 '15 at 22:11

5 Answers5

35

You should use this method

 Route::get('/logout', function() {
 Session::forget('key');
  if(!Session::has('key'))
   {
      return "signout";
   }
 });
Imtiaz Pabel
  • 5,307
  • 1
  • 19
  • 24
  • How do I do this automatically after a specific time interval, delete specific session key after set time interval. Not the entire session. – Vicky Dev Aug 07 '20 at 13:11
8

You can try with Session::pull('key');

And if you want to delete all sessions variable you can use Session::flush();

http://laravel.com/docs/5.0/session#session-usage

Clément Rigo
  • 420
  • 1
  • 3
  • 10
2
    namespace App\Http\Controllers;

    use Illuminate\Http\Request;

    use App\Http\Controllers\Controller;

    class StudentRecord extends Controller
       {

    public function logout(Request $req)
        {
     if($req-session()->has('key'){
              $req->session()->forget('key');
        return redirect('/');
              }

//Or simple
    public function logout(Request $req)
        {
     //if($req-session()->has('key')
         $req->session()->flush();
         }


    }


    //Then Do this in your route file
      Route:get("/logout",StudentRecord@logout);

        <a href=" {{ url(logout)}}">logout</a>
Sumit patel
  • 3,807
  • 9
  • 34
  • 61
0

Session::forget() does not return true/false. You can just remove your if statement.

As a side note, if you're only using the user key in Session for storing the currently logged in user, you can just use Auth::user() instead.

  • It is all pretty well documented in the official docs: http://laravel.com/docs/5.0/authentication – dylan_the_wizard Jun 07 '15 at 15:37
  • `Too few arguments to function Illuminate/Session/Store::forget(), 0 passed in /home/vagrant/code/uscutter/vendor/laravel/framework/src/Illuminate/Support/Manager.php on line 144 and exactly 1 expected` – Yevgeniy Afanasyev Jun 07 '18 at 03:01
0

You can use the Request parameter, containing the current session. This way you can delete any session value by the key:

use Illuminate\Http\Request;

Route::get('/logout', function(Request $request) {
        //Uncomment to see the logs record
        //\Log::info("Session before: ".print_r($request->session()->all(), true));
        if ($request->session()->has('key')) {
           $request->session()->forget('key');
        }
        //Uncomment to see the logs record
        //\Log::info("Session after: ".print_r($request->session()->all(), true));
        return redirect('/');
    });

Or you can delete all values in the session:

use Illuminate\Http\Request;

Route::get('/logout', function(Request $request) {
        //Uncomment to see the logs record
        //\Log::info("Session before: ".print_r($request->session()->all(), true));
        $request->session()->flush();
        //Uncomment to see the logs record
        //\Log::info("Session after: ".print_r($request->session()->all(), true));
        return redirect('/');
    });

Reference: https://laravel.com/docs/5.3/session#using-the-session

J.C. Gras
  • 4,934
  • 1
  • 37
  • 44