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

Ved21212's avatar

Session does not regenerate after login..

Hello team, I am trying to logout and generate a new session id on logout but fail to do so..can anyone help me with that?

Following is what I have tried, Please correct me if I am wrong!! Login:

  public function LoginFront(Request $request){
        $attempt = [];
        $login_email = $request->login_email;
        $login_password = $request->login_password;

        $attempt = Auth::attempt(array('email' => $login_email, 'password' => $login_password));

        //dd($attempt);

        if($attempt == true){
            $ecomm_id = Auth::user()->id;
			
			if($ecomm_id == 1){			
				$attempt = false;
            	return response()->json($attempt);	
			}else{
                
                $request->session()->regenerate();
				$request->session()->put('ecomm_id',$ecomm_id);
            	return response()->json($attempt);
			}
            
        }else{
            return response()->json($attempt);
        }

    }

Logout:

 public function LogoutFront(Request $request){

            Auth::logout();
            Session::flush();
            $request->session()->regenerate();
                        return redirect('/');
            // return '1';

        }

Thanks

0 likes
7 replies
bugsysha's avatar

I am trying to logout and generate a new session id on logout

Why are you trying that?

Ved21212's avatar

@bugsysha My project is bascially on ecommerce.. and the scenario is that when a product is added by the logged in user to the cart it is visible in cart after logout too..

bugsysha's avatar

I've never used regenerate method on sessions. Try something like:

public function LogoutFront(Request $request)
{
	Auth::logout();
	$request->session()->flush();
	$request->session()->put('key', $data); // $data is the data you want to store so replace that.
	return redirect('/');
}
Ved21212's avatar

But what shall id pass in $data . as session id is generated automatically ?

$request->session()->put('session_id', $data);
bugsysha's avatar
bugsysha
Best Answer
Level 61

You put a piece of information that will help you link that session with what is in the cart. You do not force session ID.

Please or to participate in this conversation.