Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

nam_co's avatar

Session id at login changes

Hello, Im trying to pass the session id to the /listeners/LogSuccessfulLogin, so when the user logins it's store in the DB, but when I complete the login and echo the session()->getId() in the view, it's different, is there any way to get the correct id in the listener?

Providers\EventServiceProvider

    protected $listen = [
        'Illuminate\Auth\Events\Login' => [
            'App\Listeners\LogSuccessfulLogin',
        ],
    ];

Listeners\LogSuccessfulLogin


    public function __construct(Request $request)
    {
        $this->request = $request;
    }
    public function handle(Login $event)
    {
        $id = $event->user->id; //this works
        //$session = (auth()->check()) ? auth()->session()->getId() : null;
        $session = session()->getId(); //this is the wrong id
    }

Appreciate any help

0 likes
12 replies
Cronix's avatar

sessionid is the id of the session, not the user

Try Auth::user()->id;

jekinney's avatar

As @Cronix said: you're comparing two different ids. Session id is random while obviously user id is auto incrementing.

Each id will almost always 99.9% of the time be different.

Each time a user logs in a new session is created with a new expiration date etc.

For testing and better understanding use db for sessions and keep an eye on it. You'll see it update etc.

nam_co's avatar

Hi Cronix, thanks for the response, I'm trying to get the session id, I've read that the session changes when the user is logged , so I'm trying to get the new session id

Snapey's avatar

what practical use can you have for the session id? It can change at any time.

nam_co's avatar

I'm trying to register user logins in the DB, so there's a columns for login and logout datetime, when the user logout will search the DB row using the user and session to update it

Snapey's avatar

You may never know that the user has logged out because they can just close their browser or go away for days and weeks, by which time their session token will no longer be valid. You are building functionality that will never really give you what you want?

nam_co's avatar

True, but it will work sometimes, I tried with ip but if the user is in mobile for example and passes from 4G to wifi the ip changes, so I'm trying to find a way to use the session

Snapey's avatar
Snapey
Best Answer
Level 122

Then just create your own token (or the ID from your logins table) and save it in the session.

You can then always retrieve it at logout.

nam_co's avatar

true, but I just wish to find a way, but I will probably end up doing session(['user_login' => uniqid()]);

nam_co's avatar

Snapey if I use ( 'Illuminate\Auth\Events\Logout', 'App\Listeners\UserEventSubscriber@onUserLogout' ) will this event be activated when lifetime or expire_on_close are call?

Appreciate the help

Snapey's avatar

It will only be called if the user clicks logout.

The server is doing nothing in the background when there are no requests, and when there are requests, randomly, expired sessions are cleaned up by deleting the session records. I would be surprised if the event is called then but admit I don't know?

Please or to participate in this conversation.