Home > php教程 > PHP源码 > body text

A very basic SESSION management class in singleton mode

大家讲道理
Release: 2016-11-09 10:11:15
Original
1360 people have browsed it

<?php
 
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;
    }
}
Copy after login

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template