thinkphp session控制一段时间内无操作自动跳转

浏览:1670 发布日期:2017/04/17 分类:功能实现 关键字: session
thinkphp 防止非法用户绕开登录界面,一段时间后无操作自动跳转登录界面
//获取当前登录
function loginEmployeeid() {
    $employee = session('employee');
    //$class = new \Org\Util\Session();
    //$employee = $class->get("employee");
    if (empty($employee)) {
        return 0;
    } else {
     
        $last_access=session('last_access');
        
        if($last_access!=null&&(time()-$last_access)<=10){
             session('last_access', time());
             return 1;
        }else{
             return 0;
        }
    }
}
<?php

namespace Home\Controller;
use Think\Controller;
class AdminController extends Controller {
         protected function _initialize(){
               if (!loginEmployeeid()) {
                    //操作方法
                    $actionName = strtolower(ACTION_NAME);
                    
                    //操作控制器
                    $controllerName = strtolower(CONTROLLER_NAME);
                        
                    if(!in_array($actionName, array("login","loginhandle"))&&!in_array($controllerName, array("index"))) {
                        $this->redirect('Home/Index/login');
                        exit;
                    }   
               }
         }
}
<?php

namespace Home\Controller;

use Think\Controller;

class IndexController extends AdminController {
    
    //首页
    public function index() {      
        $this->display();
    }
    
    //登录界面
    public function login() {
          $this->display();
    }
    
    //退出系统处理
    public function outHandle(){
        session(null);
      
        $msg['msg']  = "success";
        $this->ajaxReturn($msg,"JSON");
    }
    
    
    
    //登录系统处理
    public function loginHandle(){
        
        session("employee",time());
        session('last_access',time());
        $msg['msg']  = "success";
        $this->ajaxReturn($msg,"JSON");
    }
    
    
    public function A(){
       $this->display();
    }
    
    public function B(){
      $this->display();
    } 
}
<?php

return array(    
    'SESSION_OPTIONS'         =>  array(
        'name'                =>  'employee',                      //设置session名
        'expire'              =>  10,                              //秒
        'use_trans_sid'       =>  1,                               //跨页传递
        'use_only_cookies'    =>  0,                               //是否只开启基于cookies的session的会话方式    
        ),    
    'DEFAULT_MODULE'        =>  'Home',       // 默认模块
    'DEFAULT_CONTROLLER'    =>  'Index',       // 默认控制器名称
    'DEFAULT_ACTION'        =>  'index',       // 默认操作名称
 
    //数据库配置信息
    'DB_TYPE' => 'mysql', // 数据库类型
    'DB_HOST' => 'localhost', // 服务器地址
    'DB_NAME' => 'gd', // 数据库名
    'DB_USER' => 'root', // 用户名
    'DB_PWD' => 'root', // 密码
    'DB_PORT' => 3306, // 端口
    'DB_PREFIX' => 'think_', // 数据库表前缀 
);
评论( 相关
后面还有条评论,点击查看>>