Laravel Session :: getId()在登录和注销时有所不同

编程入门 行业动态 更新时间:2024-10-17 05:29:35
本文介绍了Laravel Session :: getId()在登录和注销时有所不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有表user_logins来维护有关用户的多个登录信息

user_logins的表结构:

'session_id', 'user_id', 'ip_address', 'user_agent', 'browser_name', 'location', 'login_at', 'is_active'

我有 UserEventSubscriber 侦听器,具有以下两个功能:

public function handleUserLogin($event) { UserLogin::create([ 'session_id'=>Session::getId(), ... ]); }

我在handleUserLogin函数中获得了Session::getId() = mpT6RDsl54JExkejrqf3fnYiFLzbR2pTb2qfNHBe

现在,当用户注销时,我想从user_logins表中更新/删除表条目,其中session_id = Session :: getId()

public function handleUserLogout($event) { dd(Session::getId()); //UserLogin::where('session_id',Session::getId())->delete(); }

handleUserLogout函数中,我获得了不同的会话ID

Session::getId() qLYngAx1Vs8VBhxm0oCKZO3fDwun02UEXRyDm0Hi,因此我无法更新/删除user_logins表中的条目

我已经看到sessions表的ID为qLYngAx1Vs8VBhxm0oCKZO3fDwun02UEXRyDm0Hi,这与我退出登录功能的ID相同.

所以我的问题是,为什么我在用户登录功能上得到了不同的会话ID?以及我应该如何使用handleUserLogin和handleUserLogout函数中的Session::getId()获取相同的会话ID.

解决方案

我发现,需要从 Auth/LoginController

中的AuthenticatesUsers

$request->session()->regenerate()登录后会生成新的会话ID,因此,如果要获得与以前相同的ID,请将该行注释掉.

protected function sendLoginResponse(Request $request) { // $request->session()->regenerate(); <-- this line is generating new session after user login $this->clearLoginAttempts($request); return $this->authenticated($request, $this->guard()->user()) ?: redirect()->intended($this->redirectPath()); }

i have table user_logins to maintain multiple login information about user

table structure for user_logins :

'session_id', 'user_id', 'ip_address', 'user_agent', 'browser_name', 'location', 'login_at', 'is_active'

i have UserEventSubscriber listener with 2 functions like below :

public function handleUserLogin($event) { UserLogin::create([ 'session_id'=>Session::getId(), ... ]); }

im getting Session::getId() = mpT6RDsl54JExkejrqf3fnYiFLzbR2pTb2qfNHBe in handleUserLogin function

now when user logout i want to update / delete the table entry from user_logins table where session_id = Session::getId()

public function handleUserLogout($event) { dd(Session::getId()); //UserLogin::where('session_id',Session::getId())->delete(); }

on handleUserLogout function im getting a different session id

Session::getId() qLYngAx1Vs8VBhxm0oCKZO3fDwun02UEXRyDm0Hi so im unable to update/delete entry in user_logins table

i have seen sessions table have id qLYngAx1Vs8VBhxm0oCKZO3fDwun02UEXRyDm0Hi which is same as id i get on logout function .

so my question is why im getting a different session id on user login function ? and what should i do to get same session id using Session::getId() in handleUserLogin and handleUserLogout function .

解决方案

I have found that , need to override sendLoginResponse from AuthenticatesUsers in Auth/LoginController

$request->session()->regenerate() generates new session id after login , so if you want to get same id as before , comment out that line .

protected function sendLoginResponse(Request $request) { // $request->session()->regenerate(); <-- this line is generating new session after user login $this->clearLoginAttempts($request); return $this->authenticated($request, $this->guard()->user()) ?: redirect()->intended($this->redirectPath()); }

更多推荐

Laravel Session :: getId()在登录和注销时有所不同

本文发布于:2023-07-19 08:45:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1154843.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Laravel   Session   getId

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!