亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

php登錄超時(shí)檢測(cè)功能實(shí)例詳解

 更新時(shí)間:2017年03月21日 10:29:21   作者:Mr_Azaz  
這篇文章主要介紹了php登錄超時(shí)檢測(cè)功能實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下

php登錄超時(shí)檢測(cè)功能實(shí)例詳解

前言:

php登錄超時(shí)問題,當(dāng)用戶超過一定時(shí)間沒有操作頁面時(shí)自動(dòng)退出登錄,原理是通過js進(jìn)行訪問判斷的!代碼如下(以thinkphp5.0版本為例)

1、創(chuàng)建登錄版塊控制器:

<?php
namespace app\manage\control;
use \think\Controller;

class Main extends Controller{

 protected $request;

 public function _initialize(){
 $this->request = \think\Request::instance();
 }

 public function login(){
 if($this->request->method() == "POST"){
 $data = $this->request->param();
   //這里為登錄驗(yàn)證(自行補(bǔ)充)
   .......
   //通過登錄提交的信息獲取數(shù)據(jù)庫(kù)中的用戶,并記錄ID($id)
   cookie('ADMIN_ID',$result["id"]);//cookie緩存
   cookie('LOGIN_TIME',Request::instance()->time()+3600);//記錄登錄時(shí)間,并緩存1小時(shí)

 }
 return view();
 }
 
 // 檢測(cè)是否登錄超時(shí)(js調(diào)用,url為:http://您的域名/manage/main/loginLosetime)
 public function loginLosetime(){
 $logintime = cookie('LOGIN_TIME');
 $time = request()->time();
 if($time > $logintime){
 return json(['code'=>1,'msg'=>'登錄超時(shí)!','url'=>url('main/login')]);
 }else{
 return json(['code'=>0]);
 }
 }

}

2、創(chuàng)建公共控制器(所有需要驗(yàn)證登錄的控制器都繼承該控制器)

<?php

namespace app\common\control;
use \think\Controller;
class AdminBase extends Controller{
 protected $request;
 public function _initialize(){
 parent::_initialize();
  $this->request = \think\Request::instance();
 $this->checkLogin();//檢測(cè)登錄
 $this->doAction();//記錄動(dòng)作
 }
 protected function checkLogin(){

 $cookie_admin_id = cookie('ADMIN_ID');
 if(!empty($cookie_admin_id)){
 //獲取登錄用戶信息
   .......
 }else{
 if($this->request->isAjax()){
 return $this->error('您還沒有登錄!',url('main/login'));
 }else{
 header("Location:".url("main/login"));
 exit();
 }
 }
 }
 // 頁面操作記錄
 protected function doAction(){
 $logintime = cookie('LOGIN_TIME');//獲取緩存登錄超時(shí)時(shí)間
 $time = request()->time();//當(dāng)前時(shí)間
  //判斷當(dāng)前時(shí)間是否大于緩存時(shí)間 或者 超時(shí)時(shí)間小于60秒后,自動(dòng)多加1個(gè)小時(shí)時(shí)間
 if($time > $logintime || ($time - $logintime) < 60){
 $newLogintime = $logintime + 3600;
 cookie('LOGIN_TIME',$newLogintime);
 }
 }
}

3、js文件

$.ajaxSetup({
 cache: false
});
$(function(){
 setInterval(function() {
 loginLosetime()
 }, 360000);//設(shè)置1小時(shí)自動(dòng)執(zhí)行 loginLosetime 函數(shù)(時(shí)間可自行調(diào)整)
});
// 登錄超時(shí)檢測(cè)
function loginLosetime(){
 $.get(AJAX_URL+'main/loginLosetime',function(res){
 if(res.code == 1){
 window.location.href = res.url;
 }
 });
}

最后在所有的頁面調(diào)用上訴js文件即可,登錄頁面可不用調(diào)用!

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

相關(guān)文章

最新評(píng)論