PHP

Simple Session Management Class in PHP

Let’s see a simple session wrapper class implemented using singleton pattern:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
    SSMC - Simple SessionManagement Class
    Copyright (C) 2012  Karpouzas George
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
class session{
	private static $_instance;
 
	private function __construct() 
	{
		if(!isset($_SESSION) && (session_id() == '')) {
     		session_start();
     		session_regenerate_id(true);
     		session_name(md5(uniqid (rand(), true)));
		}
	}
 
	/**
	 * 
	 * get input class instance (only one is created)
	 */
	public static function Instance()
	{
	    if (!self::$_instance)
	    {
	        self::$_instance = new session();
	    }
 
	    return self::$_instance;
	}
 
	/**
	 * 
	 * Get Session param value
	 * @param string $index
	 */
	public function get($index) {
		if(!isset($_SESSION)) return false;
		if (!empty($index) && !empty ( $_SESSION )) {
			if(isset($_SESSION[$index])){
				return filter_var($_SESSION[$index], FILTER_SANITIZE_STRING);
			}
		}
		return false;
	}
 
	/**
	 * 
	 * set session param
	 * @param string $index
	 * @param mixed $value
	 */
	public function set($index, $value) {
		if(!isset($_SESSION)) return false;
		if (!empty($index)) {
			$_SESSION[$index] = $value;
			return true;
		}
		return false;
	}
 
	/**
	 * 
	 * remove a session param and return its value
	 * @param string $index
	 */
	public function remove($index){
		if(!isset($_SESSION)) return false;
		if (!empty($index) && !empty ( $_SESSION )) {
			if(isset($_SESSION[$index]))
				$value = filter_var($_SESSION[$index], FILTER_SANITIZE_STRING);
				unset($_SESSION[$index]);
				unset($index);
				return $value;
		}
		return false;
	}
 
	/**
	 * 
	 * delete a session param
	 * @param string $index
	 */
	public function delete($index){
		if(!isset($_SESSION)) return false;
		if (!empty($index) && !empty ( $_SESSION )) {
			if(isset($_SESSION[$index]))
				unset($_SESSION[$index]);
				unset($index);
				return true;
		}
		return false;
	}
 
	/**
	 * 
	 * destroy everything
	 */
	public function destroy(){
		session_unset();
   		session_destroy();
	}
}