Artisan command for clearing all session data in Laravel

前端 未结 9 917
北荒
北荒 2021-02-04 02:16

What is the artisan command for clearing all session data in Laravel, I\'m looking for something like:

$ php artisan session:clear

But apparent

相关标签:
9条回答
  • 2021-02-04 02:39

    If you are using the database for session driver, then empty the sessions table. Regenerating the key will cause a lot of problems if you are using single login on many subdomains. Emptying the session table helps reduce the useless data in the session table. You can delete cookies on everyone's browswer.

    0 讨论(0)
  • 2021-02-04 02:40

    This thread is quite much old. But I would like to share my implementation of removing all sesssions for file based driver.

            $directory = 'storage/framework/sessions';
            $ignoreFiles = ['.gitignore', '.', '..'];
            $files = scandir($directory);
    
            foreach ($files as $file) {
                if(!in_array($file,$ignoreFiles)) unlink($directory . '/' . $file);
            }
    

    Why I have not used linux command 'rm'?

    Because PHP is one of the prerequisites for Laravel and Linux is not. Using this Linux command will make our project implementable on Linux environment only. That's why it is good to use PHP in Laravel.

    0 讨论(0)
  • 2021-02-04 02:48

    The problem is that PHP's SessionHandlerInterface does not force session drivers to provide any kind of destroyAll() method. Thus, it has to be implemented manually for each driver.

    Taking ideas from different answers, I came up with this solution:

    1. Create command
    php artisan make:command FlushSessions 
    
    1. Create class in 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();
        }
    }
    
    1. Run command
    php artisan session:flush
    

    Implementations for other drivers are welcome!

    0 讨论(0)
  • 2021-02-04 02:50

    If you use database session , just delete all data on that table. In my case it is 'sessions' table.

    0 讨论(0)
  • 2021-02-04 02:51

    An easy way to get rid of all sessions is to change the name of the session cookie. This can be easily done by changing the 'cookie' => '...' line in config/session.php file.

    This works independently of the session storage you use and also won't touch any other data except the session data (and thus seems preferable over the renewing the app key solution to me, where you would loose any encrypted data stored in the app).

    0 讨论(0)
  • 2021-02-04 02:53

    My solution Laravel

    // SESSION_DRIVER=file
    
    
    $files = File::allFiles(storage_path('framework/sessions/'));
    foreach($files as $file){
      File::delete(storage_path('framework/sessions/'.$file->getFilename()));
    }
    //OR
    
    //SESSION_DRIVER=redis
    
    Artisan::call('cache:clear'); // == php artisan cache:clear 

    0 讨论(0)
提交回复
热议问题