vancake vancake - 5 months ago 14
PHP Question

Session not really flush,why?

I met a problem.here are my codes(the codes are in Controller):

public function login(Request $request){

$name = $request->name;
$pwmd5 = md5($request->pw);
$result= user::select(DB::raw("CASE WHEN password ='$pwmd5' THEN 1 ELSE 0 END AS passwrod_matches,id,name"))
->where('name',$name)
->first();

if ($result == null) {
return returnFormat('account wrong', false);
} elseif ($result['password_matches'] == '0') {
return returnFormat('pw wrong', false);
};
$result['password_status'] = '1';
static::saveUser($result);
return returnFormat($result);
}

static public function saveUser(&$user){

if(!is_array($user)){
$user->toArray();
}
$user['sid'] = Session::getId();
session(['user'=>$user]);

}

public function checkUser(Request $request){

$user = session('user');
return $user;

}

public function logout(Request $request){


session()->flush();
return returnFormat(true);

}





what I did is login()-> checkuser()-> logout();
But when I login() with another account,I found that the session Id is exactly the same..why?

Answer

Three ways to destroy sessions :

1)

Session::flush(); // removes all session data

2)

Session::forget('yourKeyGoesHere') // Removes a specific variable

3)

Auth::logout() // logs out the user
Comments