$ php artisan session:clear
但显然它不存在。我该如何在命令行中清除它?
我尝试使用
$ php artisan tinker
...
\Session::flush();
但它只刷新了一个用户的会话,我想要清除所有用户的所有会话。我该怎么做?
我尝试了以下代码:
artisan cache:clear
但是它并没有清除会话。
$ php artisan session:clear
但显然它不存在。我该如何在命令行中清除它?
我尝试使用
$ php artisan tinker
...
\Session::flush();
但它只刷新了一个用户的会话,我想要清除所有用户的所有会话。我该怎么做?
我尝试了以下代码:
artisan cache:clear
但是它并没有清除会话。
如果您正在使用基于文件的会话,则可以使用以下Linux命令清除会话文件夹:
rm -f storage/framework/sessions/*
.gitignore
文件,该文件用于在提交时保留(空)文件夹。 - Clément Baconnier更新:这个问题似乎经常被问到,并且许多人仍在积极评论。
在实践中,使用
session_flush()
来清除会话是一个可怕的想法。
php artisan key:generate
这可能会造成各种问题。最好的方法是清除您正在使用的任何系统。
懒程序员清除所有会话的指南:
php artisan key:generate
由于指定了新的应用程序密钥,将使所有会话无效。
不那么懒惰的方法
php artisan make:command FlushSessions
然后插入
<?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();
}
}
然后
php artisan session:flush
php artisan make:command FlushSessions
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();
}
}
php artisan session:flush
\Session::getHandler()->gc(0)
。 - adjustment layer\Session::getHandler()->gc(0); // Destroy all sessions which exist more than 0 minutes
一个简单的方法来清除所有会话是更改会话cookie的名称。这可以通过更改config/session.php
文件中的'cookie' => '...'
行轻松完成。
这与您使用的会话存储无关,也不会触及任何其他数据,仅影响会话数据(因此对我来说似乎比更新应用程序密钥解决方案更可取,因为后者会丢失存储在应用程序中的任何加密数据)。
APP_KEY
也是使用Hash::make()
创建哈希时的盐。因此,在更改它后,用户密码和任何其他哈希数据都将无效。 - algorhythm我的解决方案
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
我知道这是一个老旧的线程,但对我有用的方法就是删除cookies。
在Chrome中,进入开发控制台,转到“应用程序”选项卡。在侧边栏中找到“Cookies”,并单击其前面的小箭头。进入您的域名,单击过滤字段旁边的图标以清除您域名的cookies。刷新页面,所有会话数据都是新的,旧的数据都被删除。
这个帖子很旧了,但我想分享一下我的实现方式,可以移除基于文件的驱动程序中的所有会话。
$directory = 'storage/framework/sessions';
$ignoreFiles = ['.gitignore', '.', '..'];
$files = scandir($directory);
foreach ($files as $file) {
if(!in_array($file,$ignoreFiles)) unlink($directory . '/' . $file);
}
为什么我没有使用Linux命令“rm”?
因为Laravel需要PHP作为先决条件,而不是Linux。使用这个Linux命令将使我们的项目只能在Linux环境中实现。 这就是为什么在Laravel中使用PHP很好的原因。
如果您正在使用数据库作为会话驱动程序,则请清空会话表。如果您在许多子域上使用单个登录,则重新生成密钥将导致许多问题。清空会话表有助于减少会话表中的无用数据。您可以删除每个人浏览器上的cookie。
如果您使用数据库会话,请只需删除该表上的所有数据。在我的情况下,它是 'sessions' 表。