Do the user exit login, Ajax call Delete session login set $request->session ()->put (' User ', ' value ');
Use $request->session ()->forget (' user ') and jump to login page
However, we found that the session data obtained after the jump is still the session state before deletion, so we still keep the login status. Is my usage wrong?
Here's how Ajax calls:
public function logout(Request $request) { if ($request->session()->has('user')) { $is_forgotten = $request->session()->forget('user'); if ($is_forgotten === null) echo json_encode(['result' => 1]); exit; } echo json_encode(['result'=>0,'msg'=>'退出失败']); exit; }
Here is the code for the user assignment to the session at login
public function valid(Request $request) { $r = $request->all(); $validator = Validator::make($r,[ 'username'=>'bail|required', 'password'=>'bail|required|integer', ]); if ($validator->fails()) { return redirect('admin/login')->withErrors($validator)->withInput(); } $where = [ 'name'=>$r['username'], 'pwd'=>md5($r['password']), ]; $hasExists = AdminModel::hasExists($where); if (null !== $hasExists) { $request->session()->put('user',json_encode(['name'=>$r['username']])); return redirect('admin'); } else { return redirect('admin/login')->with('hasExists','用户名或密码错误'); } }
Reply content:
Do the user exit login, Ajax call Delete session login set $request->session ()->put (' User ', ' value ');
Use $request->session ()->forget (' user ') and jump to login page
However, we found that the session data obtained after the jump is still the session state before deletion, so we still keep the login status. Is my usage wrong?
Here's how Ajax calls:
public function logout(Request $request) { if ($request->session()->has('user')) { $is_forgotten = $request->session()->forget('user'); if ($is_forgotten === null) echo json_encode(['result' => 1]); exit; } echo json_encode(['result'=>0,'msg'=>'退出失败']); exit; }
Here is the code for the user assignment to the session at login
public function valid(Request $request) { $r = $request->all(); $validator = Validator::make($r,[ 'username'=>'bail|required', 'password'=>'bail|required|integer', ]); if ($validator->fails()) { return redirect('admin/login')->withErrors($validator)->withInput(); } $where = [ 'name'=>$r['username'], 'pwd'=>md5($r['password']), ]; $hasExists = AdminModel::hasExists($where); if (null !== $hasExists) { $request->session()->put('user',json_encode(['name'=>$r['username']])); return redirect('admin'); } else { return redirect('admin/login')->with('hasExists','用户名或密码错误'); } }
Suggest landlord in understanding Larave of the session mechanism, if your code midway has exit, preferably before it session::save () a bit