0
我正在使用OOP登錄/註冊系統,我想申請會話超時,我看了很多教程,並且他們都解釋瞭如何去關於它只使用普通的登錄/註冊系統。這裏是我的一些代碼:用戶登錄會話超時註銷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;
}
}