使用OOP登录/注册系统的用户登录会话超时注销

时间:2017-04-21 13:54:12

标签: php oop session

我正在使用 OOP 登录/注册系统,我想应用会话超时,我看了很多教程,所有这些都解释了如何使用正常登录进行操作/仅限注册系统。这是我的一些代码:

的init.php

session_start();
 $GLOBALS['config'] = array(
    'mysql' => array(
        'host'      =>  '127.0.0.1',
        'username'  =>  'root',
        'password'  =>  '',
        'db'        =>  'database'   
    ),

    'remember'  =>  array(
        'cookie_name'   =>  'hash',
        'cookie_expiry' =>  '604800'
    ),

    'session'   =>  array(
        'session_name'  =>  'user',
        'token_name'    =>  'token'
    )
);

以下是处理登录的课程

session.php文件

class Session {
public static function exists($name){
    return (isset($_SESSION[$name])) ? true : false;
}

public static function put($name, $value){
    return $_SESSION[$name] = $value;
}

public static function get($name){
    return $_SESSION[$name];
}

public static function delete($name){
    if(self::exists($name)){
        unset($_SESSION[$name]);
    }
}

public static function flash($name, $string = ''){
    if(self::exists($name)){
        $session = self::get($name);
        return $session;
    }else{
        self::put($name, $string);
    }
}

}

user.php的

class User{
private $_db,
        $_data,
        $_sessionName,
        $_isLoggedIn;


public function __construct($user = null) {
    $this->_db = DB::getInstance();

    $this->_sessionName = Config::get('session/session_name');

    if(!$user){
        if(Session::exists($this->_sessionName)){
            $user = Session::get($this->_sessionName);

            if($this->find($user)){
                $this->_isLoggedIn = true;
            }else{
                //process logout
            }
        }    
    }else{
        $this->find($user);
    }
}

public function user_password($password = null){
    if($this->data()->password === Hash::make($password, $this->data()->salt)){
        return true;
    }
}


public function find($user = null){
    if($user){
        $field = (is_numeric($user)) ? 'user_id' : 'username';
        $data = $this->_db->get('users', array($field, '=', $user));
        if($data->count()){
           $this->_data = $data->first();
            return true; 
        }
    }
    return false;
}

public function login($username = null, $password = null){
    $user = $this->find($username);

    if($user){
        if($this->data()->password === Hash::make($password, $this->data()->salt)){
            //This palce is important, change the user_id to the name of the users' table primary key
            Session::put($this->_sessionName, $this->data()->user_id);
            return true;
        }
    }
    return false;
}

public function hasPermission($key){
    $group = $this->_db->get('groups', array('id', '=', $this->data()->group));

    if($group->count()){
        $permissions = json_decode($group->first()->permissions, true);

        if($permissions[$key] == true){
            return true;
        }
    }
    return false;
}

public function logout(){
    Session::delete($this->_sessionName);
}

public function data(){
    return $this->_data;
}

public function isLoggedIn(){
    return $this->_isLoggedIn;
}

}

1 个答案:

答案 0 :(得分:0)

如果登录成功,您可以添加 $_SESSION['timeOfLogin'] = time();

执行任何任务时,您可以通过将 $_SESSION['timeOfLogin'] 发送到以下功能来检查登录是否仍然有效

 public function sessionIsValid($timeOfLogin){
    //Lets say expiry is 1 day
    $expiryTime=24*60*60; 
    //Get the current time
    $currentTime = time();
   // How much time user has spent
    $timeDifference = $currentTime - timeOfLogin;
   //If time spent is less than expiry then it is good to go
    if($timeDifference < $expiryTime)
     return true;
    else
     return false;

    }