用于清除 Laravel 中所有会话数据的 Artisan 命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50732321/
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
Artisan command for clearing all session data in Laravel
提问by Yevgeniy Afanasyev
What is the artisan command for clearing all session data in Laravel, I'm looking for something like:
清除 Laravel 中所有会话数据的工匠命令是什么,我正在寻找类似的东西:
$ php artisan session:clear
But apparently it does not exist. How would I clear it from command line?
但显然它不存在。我如何从命令行清除它?
I tried using
我尝试使用
$ php artisan tinker
...
\Session::flush();
But it flushes session of only one user, I want to flush all sessions for all users. How can I do it?
但它只刷新一个用户的会话,我想刷新所有用户的所有会话。我该怎么做?
I tried this:
我试过这个:
artisan cache:clear
But it does not clear session, again.
但它并没有清除会话,再次。
回答by Devin Gray
UPDATE: This question seems to be asked quite often and many people are still actively commenting on it.
更新:这个问题似乎经常被问到,许多人仍在积极评论。
In practice, it is a horrible idea to flush sessions using the
在实践中,使用
php artisan key:generate
It may wreak all kinds of havoc. The best way to do it is to clear whichever system you are using.
它可能会造成各种破坏。最好的方法是清除您正在使用的任何系统。
The Lazy Programmers guide to flushing all sessions:
刷新所有会话的懒惰程序员指南:
php artisan key:generate
Will make all sessions invalid because a new application key is specified
将使所有会话无效,因为指定了新的应用程序密钥
The not so Lazy approach
不那么懒惰的方法
php artisan make:command FlushSessions
and then insert
然后插入
<?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();
}
}
and then
进而
php artisan session:flush
回答by mitra razmara
If you are using file based sessions, you can use the following linux command to clean the sessions folder out:
如果您使用的是基于文件的 session,则可以使用以下 linux 命令清除 session 文件夹:
rm -f storage/framework/sessions/*
回答by miho
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.
摆脱所有会话的一种简单方法是更改会话 cookie 的名称。这可以通过更改文件中的'cookie' => '...'
行轻松完成config/session.php
。
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).
这独立于您使用的会话存储工作,并且也不会触及除会话数据之外的任何其他数据(因此对我来说似乎比更新应用程序密钥解决方案更可取,在那里您会丢失存储在应用程序中的任何加密数据)。
回答by Hamees A. Khan
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'?
为什么我没有使用 linux 命令 '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.
因为 PHP 是 Laravel 的先决条件之一,而 Linux 不是。使用这个 Linux 命令将使我们的项目只能在 Linux 环境中实现。这就是为什么在 Laravel 中使用 PHP 是好的原因。
回答by jotaelesalinas
The problem is that Laravel's SessionHandlerInterface
does not force session drivers to provide any kind of destroyAll()
method. Thus, it has to be implemented manually for each driver.
问题是 LaravelSessionHandlerInterface
不会强制会话驱动程序提供任何类型的destroyAll()
方法。因此,它必须为每个驱动程序手动实现。
Taking ideas from different answers, I came up with this solution:
从不同的答案中汲取灵感,我想出了这个解决方案:
- Create command
- 创建命令
php artisan make:command FlushSessions
- Create class in
app/Console/Commands/FlushSessions.php
- 在创建类
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 command
- 运行命令
php artisan session:flush
Implementations for other drivers are welcome!
欢迎其他驱动程序的实现!