Artisan命令用于清除Laravel中的所有会话数据

Yev*_*yev 6 session laravel artisan

什么是清除Laravel中所有会话数据的artisan命令,我正在寻找类似的东西:

$ php artisan session:clear
Run Code Online (Sandbox Code Playgroud)

但显然它不存在.我如何从命令行清除它?

我试过用

$ php artisan tinker  
...
\Session::flush();
Run Code Online (Sandbox Code Playgroud)

但它只刷新一个用户的会话,我想为所有用户刷新所有会话.我该怎么做?

我试过这个:

artisan cache:clear
Run Code Online (Sandbox Code Playgroud)

但它还没有明确会话.

Dev*_*ray 15


更新:这个问题似乎经常被问到,许多人仍在积极评论它.

在实践中,使用the刷新会话是一个可怕的想法

php artisan key:generate
Run Code Online (Sandbox Code Playgroud)

它可能会造成各种各样的破坏.最好的方法是清除您使用的系统.


懒惰程序员指导刷新所有会话:

php artisan key:generate
Run Code Online (Sandbox Code Playgroud)

将使所有会话无效,因为指定了新的应用程序密钥

不那么懒惰的方法

php artisan make:command FlushSessions
Run Code Online (Sandbox Code Playgroud)

然后插入

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use DB;

class flushSessions extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'session:flush';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Flush all user sessions';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        DB::table('sessions')->truncate();
    }
}
Run Code Online (Sandbox Code Playgroud)

然后

php artisan session:flush
Run Code Online (Sandbox Code Playgroud)

  • 此外,答案的第二部分仅在将会话存储到数据库时有效.请注意,通常情况并非如此(例如,如果您使用Redis会话存储或使用cookie会话存储). (5认同)
  • 请注意,生成新的应用密钥会破坏您在Laravel中加密的任何其他数据. (3认同)
  • 乐于注销您的用户:D (2认同)
  • 不要仅仅因为您想摆脱会话而使生产应用程序中的应用程序密钥无效。它还将使所有加密(而不是散列)不可读。也许您的应用程序没有存储任何加密的数据,但是在StackOverflow上向陌生人推荐它似乎不是一个好主意。 (2认同)

adj*_*yer 14

如果您想完全删除任何驱动程序的会话。使用这段代码

\Session::getHandler()->gc(0); // Destroy all sessions which exist more than 0 minutes
Run Code Online (Sandbox Code Playgroud)


mit*_*ara 11

如果您使用的是基于文件的会话,则可以使用以下linux命令清除会话文件夹:

rm -f storage/framework/sessions/*
Run Code Online (Sandbox Code Playgroud)

  • 请提供解释,我的意思是不要成为artisan命令,但仅当您为会话数据使用文件驱动程序时,它才有效。 (2认同)
  • 这也会删除`.gitignore`文件,该文件在提交时会保留(空)文件夹。 (2认同)

jot*_*nas 8

问题是 PHPSessionHandlerInterface不会强制会话驱动程序提供任何类型的destroyAll()方法。因此,它必须为每个驱动程序手动实现。

从不同的答案中汲取灵感,我想出了这个解决方案:

  1. 创建命令
php artisan make:command FlushSessions 
Run Code Online (Sandbox Code Playgroud)
  1. 在创建类 app/Console/Commands/FlushSessions.php
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class FlushSessions extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'session:flush';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Flush all user sessions';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $driver = config('session.driver');
        $method_name = 'clean' . ucfirst($driver);
        if ( method_exists($this, $method_name) ) {
            try {
                $this->$method_name();
                $this->info('Session data cleaned.');
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }
        } else {
            $this->error("Sorry, I don't know how to clean the sessions of the driver '{$driver}'.");
        }
    }

    protected function cleanFile () {
        $directory = config('session.files');
        $ignoreFiles = ['.gitignore', '.', '..'];

        $files = scandir($directory);

        foreach ( $files as $file ) {
            if( !in_array($file,$ignoreFiles) ) {
                unlink($directory . '/' . $file);
            }
        }
    }

    protected function cleanDatabase () {
        $table = config('session.table');
        DB::table($table)->truncate();
    }
}
Run Code Online (Sandbox Code Playgroud)
  1. 运行命令
php artisan session:flush
Run Code Online (Sandbox Code Playgroud)

欢迎其他驱动程序的实现!


mih*_*iho 6

摆脱所有会话的一种简单方法是更改​​会话 cookie 的名称。这可以通过更改文件中的'cookie' => '...'行轻松完成config/session.php

这独立于您使用的会话存储工作,并且也不会触及除会话数据之外的任何其他数据(因此对我来说似乎比更新应用程序密钥解决方案更可取,在那里您会丢失存储在应用程序中的任何加密数据)。