首页 >php教程 >PHP源码 >简单的SESSION管理类

简单的SESSION管理类

PHP中文网
PHP中文网原创
2016-05-23 16:40:151393浏览

php代码:

<?php
/**
 * Created by PhpStorm.
 * User: zerodeng
 * Date: 15-5-12
 * Time: 下午3:09
 */

class session {
    const SESSION_STARTED = TRUE;
    const SESSION_NOT_STARTED = FALSE;

    private $session_state = self::SESSION_NOT_STARTED;
    private static $instance;

    public static function get_instance(){
        if(!isset(self::$instance)){
            self::$instance = new self;
        }

        self::$instance->start_session();
        return self::$instance;
    }

    public function start_session(){
        if($this->session_state == self::SESSION_NOT_STARTED){
            $this->session_state = session_start();
        }
        return $this->session_state;
    }

    public function __set($name,$value){
        $_SESSION[$name] = $value;
    }

    public function __get($name){
        if(isset($_SESSION[$name])){
            return $_SESSION[$name];
        }
    }

    public function __isset($name){
        return isset($_SESSION[$name]);
    }

    public function __unsset($name){
        unset($_SESSION[$name]);
    }

    public function destroy(){
        if($this->session_state == self::SESSION_STARTED){
            $this->session_state = !session_destroy();
            unset($_SESSION);
            return !$this->session_state;
        }

        return false;
    }
}
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn