laravel的登录和退出

admin2023-01-173737

登陆时设置session和cookie:有错误,不要用

public function DoLogin(Request $request, Users $users)
    {
        if($request->input('phone')){
            $rules = [
                "captcha" => 'required|captcha'
            ];
            $messages = [
                'cpt.required' => '请输入验证码',
                'cpt.captcha' => '验证码错误,请重试'
            ];
            //如果仅仅验证captcha的值可以
            $validator = Validator::make($request->all(), $rules, $messages);
            //判断图片验证码
            $i =$validator->fails();

            if($i == false){
                $login_phone['phone'] = $request->input("phone");
                $login_pwd = substr(md5(env('MD5_KEY','').$request->input("pwd")),7,27);
                $user = new users();
                $users = $user->get_model('users', ['phone=? and pwd = ?', [$login_phone,$login_pwd]]);
                if(!empty($users)){
                    $follow_room = new User_followroom();
                    $follow_rooms = $follow_room->get_list('user_followroom', ['user_id=?', [$users->id]]);
                    $follow_rooms = parent::objToArray($follow_rooms);
                    if($follow_rooms){
                        $rooms = array();
                        foreach($follow_rooms as $k=>$room){
                            $rooms[$k] = $room['followroom'];
                        }
                        $users->follow_rooms = $rooms;
                    }
                    Session::put("home_users",$users);
                    setcookie('users_name', $users->name, 0, '/');
                    $date['login_time'] = date("Y-m-d H:i:s");
                    $user->update_by_id('users', $date, $users->id);
                    return redirect('home/index');
                    $data = ['status' => 0,'info' => '登录成功!'];
                }else{
                    $data = ['status' => 3,'info' => '账号或密码错误!'];
                }
            }else{
                $data = ['status' => 4,'info' => '验证码错误!'];
            }
        }else{
            $data = ['status' => 5, 'info' => '账号密码不能为空!'];
        }
        return $data;
    }

退出时删除session和cookie:

public function Logout()
{
    Session()->forget('home_users');
    Cookie::queue('users_name', null , -1); // 销毁
    return back();
}


网友评论