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)
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)
问题是 PHPSessionHandlerInterface不会强制会话驱动程序提供任何类型的destroyAll()方法。因此,它必须为每个驱动程序手动实现。
从不同的答案中汲取灵感,我想出了这个解决方案:
php artisan make:command FlushSessions
Run Code Online (Sandbox Code Playgroud)
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)
php artisan session:flush
Run Code Online (Sandbox Code Playgroud)
欢迎其他驱动程序的实现!
摆脱所有会话的一种简单方法是更改会话 cookie 的名称。这可以通过更改文件中的'cookie' => '...'行轻松完成config/session.php。
这独立于您使用的会话存储工作,并且也不会触及除会话数据之外的任何其他数据(因此对我来说似乎比更新应用程序密钥解决方案更可取,在那里您会丢失存储在应用程序中的任何加密数据)。