## [PHP交流群:494826865(点击群号立即加群)](https://jq.qq.com/?_wv=1027&k=50Qy5h6) # 管理员登录补充 > 到这里管理员的登录功能就完成了,但是还有一些细节上需要做一下优化。 1. 管理员已登录时,自动跳过登录页面 2. 在后台操作时,如果登录超时的处理。 ##### 一、自动跳过登录页面 >[success] 在进入登录页面时,需要判断管理员是否登录 控制器层:`Admin/LoginController.php` ~~~    /**     * 加载登页面     * @RequestMapping(route="login", method=RequestMethod::GET)     */    public function index()   {        //判断是否登录        if($this->loginlogic->checkLoginStatus()){            return context()->getResponse()->redirect("/admin");       }        return view("admin/login/login");   } ~~~ 逻辑层:`Modle/Logic/LoginLogic.php` 新增:`checkLoginStatus`方法,用来检测管理员的登录状态 ~~~    /**     * 判断用户登录状态     */    public function checkLoginStatus(){        if(HttpSession::current()->has("username") && HttpSession::current()->has("userid")){           return true;       }        return false;   } ~~~ ##### 二、登录超时的处理 >[success] 判断是否登录超时,所有的后台操作都进行验证检测,所以在这里直接在中间件中进行验证。 在`Http/Middleware/CheckLoginMiddleware.php`添加检则代码 ~~~    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface   {        if(HttpSession::current()->has("username") && HttpSession::current()->has("userid")){            // before request handle            return $handler->handle($request);       }        //判断请求类型        if($request->isAjax()){            throw new HttpExecption("登录超时",3);       }        return context()->getResponse()->redirect("/admin/login"); ​        // after request handle   } ~~~ >[success] 添加过之后,即可进行检测,前提是控制器要经过中间件。