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

Aniket_IN's avatar

Get previous session data after successful login

Hi, I'm trying to achieve something like whenever someone login/registers I want to do something with the previous session id.

I saw laravel provides few auth events (https://laravel.com/docs/8.x/authentication#events)

// App\Listeners\LogSuccessfulLogin.php
    public function handle($event)
    {
        // It works
        dd(auth()->user());

        // But I also want to get the old/previos session id.

    }

Or please suggest some other solution to achieve this.

Thank you so much in advance :)

0 likes
4 replies
lbecket's avatar

Seeing as the previous session would be destroyed, I believe your best option will be to store the necessary information in some kind of database or data store, thus making it available at the time of the next login. Maybe something like Redis could meet your need.

Aniket_IN's avatar

@lbecket Thank you for replying, I was also thinking of something like that, but after spending a few hours I saw, on the event handle function, we can access the old session id via cookie.

Something like this:

    public function handle($event)
    {
        $oldSessionID = request()->cookie(config('session.cookie'));
        $newSessionID = session()->getId();
    }

I don't know whether I should really use it.

lbecket's avatar

@Aniket_IN I guess it depends on how critical the need is. A user could clear their cookies, so if that breaks your system then you're better off storing it on the server side.

Snapey's avatar

perhaps you could wrap the login process, copying the current session to local variables, log the user in then restore the variables to the new session

Please or to participate in this conversation.