Laravel,获取当前登录的用户
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15632144/
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
Laravel, get currently logged-in users
提问by Mission
I want to display a list of currently logged-in users in an app. I want to use Laravel Auth method. I'm looking at the API and I cannot find anything like it.
我想在应用程序中显示当前登录用户的列表。我想使用 Laravel Auth 方法。我正在查看 API,但找不到类似的东西。
I would probably need to loop through the sessions store and then match it to a user ID. Am I right?
我可能需要遍历会话存储,然后将其与用户 ID 匹配。我对吗?
UPDATE: Forgot to mention, I'm storing sessions in the DB.
更新:忘了提,我在数据库中存储会话。
回答by vFragosop
"Currently logged in" is something you can't do with plain old sessions. Let me explain why:
“当前登录”是您无法使用普通旧会话进行的操作。让我解释一下原因:
A session is a bunch of data stored at server side which is assigned to an user through a cookie. That cookie remains on user browser and so it keeps the session active. Sessions can stay "alive" months without the user even logging in.
会话是存储在服务器端的一堆数据,通过 cookie 分配给用户。该 cookie 保留在用户浏览器上,因此它使会话保持活动状态。会话可以在没有用户登录的情况下保持“活跃”数月。
But,it's possible to store sessions on database.
但是,可以在 database上存储会话。
As you can see, Laravel keeps a field called last_activity
and, through that field, you should be able to retrieve all sessions that had activity within the last 15 minutes (or something else, you call it).
如您所见,Laravel 保留了一个名为的字段last_activity
,通过该字段,您应该能够检索在过去 15 分钟内(或其他名称)有活动的所有会话。
When your retrieve those records, the data
field is a serialized representation of session data. You can unserialize($session_record->data)
and retrieve the user id.
当您检索这些记录时,该data
字段是会话数据的序列化表示。您可以unserialize($session_record->data)
检索用户 ID。
Depending on your Auth driver, session's user id may have different names:
根据您的 Auth 驱动程序,会话的用户 ID 可能具有不同的名称:
- For eloquent driver, it should be
eloquent_login
. - For fluent driver
fluent_login
. - For your
Custom\AuthClass
, it should be calledcustom_authclass_login
.
- 对于雄辩的驱动程序,它应该是
eloquent_login
. - 对于流利的司机
fluent_login
。 - 对于你的
Custom\AuthClass
,它应该被称为custom_authclass_login
.
回答by Tiefan Ju
Assume that all http requests from logged in users are passing auth
middleware, we can override terminate
function like following:
假设所有来自登录用户的 http 请求都通过auth
中间件,我们可以覆盖terminate
如下函数:
public function terminate($request, $response)
{
Auth::user()->save();
}
Then a query like User::where('updated_at', '>', Carbon::now()->subMinutes(12))->get();
will bring all logged in user, where 12
is the lifetime of session.
然后像这样的查询User::where('updated_at', '>', Carbon::now()->subMinutes(12))->get();
将带来所有登录的用户,12
会话的生命周期在哪里。
Of course, for real time, we should use ajax
calls every 5 seconds or websockets via pusher
or other.
当然,对于实时,我们应该ajax
每 5 秒使用一次调用或 websockets viapusher
或其他。
回答by Pankaj Suthar
First create a table where the logged in user's id will be inserted
首先创建一个表,其中将插入登录用户的 id
Schema::create('active_users', function(Blueprint $table)
{
$table->increments('id')->unsigned();
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')
->onUpdate('cascade')->onDelete('cascade');
$table->timestamps();
});
Then in yourcontroller insert data while logging in
然后在您的控制器中登录时插入数据
if (Auth::attempt($credentials)) {
DB::table('active_users')->insert(array('user_id' => Auth::id()));
}
and delete the data while logging out
并在注销时删除数据
DB::table('active_users')->where('user_id', '=', Auth::id())->delete();
Print the online users list in your view
在您的视图中打印在线用户列表
<ul><strong>Online Users</strong>
<?php $online_users = DB::table('active_users')->where('user_id','!=',Auth::id())->get(); ?>
@foreach($online_users as $online_user)
<li>{{User::find($online_user->user_id)->first_name}}</li>
@endforeach
</ul>