php 如何在 Laravel 5 中删除会话

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29462374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:25:16  来源:igfitidea点击:

how to remove sessions in Laravel 5

phplaravel-5

提问by rockie

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

我正在尝试删除一个基本会话,但它没有删除。这是代码

welcome.blade.php

欢迎.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.php

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

   $vv =  Session::forget('key');
   if($vv)
   {
        return "signout";
   }
});

回答by Imtiaz Pabel

You should use this method

你应该使用这个方法

 Route::get('/logout', function() {
 Session::forget('key');
  if(!Session::has('key'))
   {
      return "signout";
   }
 });

回答by Clément Rigo

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

你可以试试 Session::pull('key');

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

如果你想删除所有会话变量,你可以使用 Session::flush();

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

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

回答by dylan_the_wizard

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

Session::forget()不返回真/假。您可以删除您的if声明。

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

附带说明一下,如果您仅使用user密钥Session来存储当前登录的用户,则可以Auth::user()改为使用。

回答by J.C. Gras

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

您可以使用Request包含当前会话的参数。这样您就可以通过键删除任何会话值:

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

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

回答by Adeyemo Kabir A

    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>