1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13: namespace Pry\Session;
14:
15: 16: 17: 18: 19: 20: 21: 22: 23:
24: class Session
25: {
26:
27: 28: 29: 30: 31:
32: private $sessionName;
33:
34: 35: 36: 37: 38:
39: private $sessionTTL;
40:
41: 42: 43: 44:
45: private $useCookie;
46:
47: 48: 49: 50: 51:
52: private $regenerate;
53:
54: 55: 56: 57: 58:
59: static private $instance;
60:
61: 62: 63: 64: 65:
66: static protected $started = false;
67:
68: 69: 70: 71: 72: 73: 74: 75:
76: private function __construct($name = 'PrySess', $ttl = null, $regenerate = true)
77: {
78: if (isset($_SESSION))
79: throw new Exception('Une session existe déjà');
80:
81: $this->sessionName = $name;
82: $this->sessionTTL = $ttl;
83: $this->useCookie = (boolean) ini_get('session.use_cookies');
84: $this->autoRegenerate = $regenerate;
85:
86: session_name($this->sessionName);
87:
88: if (!self::$started)
89: {
90: session_start();
91: if ($this->regenerate)
92: {
93: session_regenerate_id();
94: }
95: self::$started = true;
96: }
97:
98: if (self::$started)
99: {
100: if (!is_null($this->sessionTTL))
101: if (empty($_SESSION['ttl']))
102: $_SESSION['ttl'] = time() + $this->sessionTTL;
103: else
104: $_SESSION['ttl'] = -1;
105: }
106: }
107:
108: 109: 110: 111: 112: 113: 114: 115:
116: public static function getInstance($name = 'PrySess', $ttl = null)
117: {
118: if (!isset(self::$instance))
119: self::$instance = new Session($name, $ttl);
120:
121: return self::$instance;
122: }
123:
124: 125: 126: 127: 128: 129: 130:
131: public function __get($name)
132: {
133: return array_key_exists($name, $_SESSION) ? $_SESSION[$name] : null;
134: }
135:
136: 137: 138: 139: 140: 141:
142: public function __isset($name)
143: {
144: return isset($_SESSION[$name]);
145: }
146:
147: 148: 149: 150: 151: 152:
153: public function __set($name, $value)
154: {
155: $_SESSION[$name] = $value;
156: }
157:
158: 159: 160: 161: 162:
163: public function refresh($delete_old = true)
164: {
165: session_regenerate_id($delete_old);
166: if (!is_null($this->sessionTTL))
167: $_SESSION['ttl'] = time() + $this->sessionTTL;
168: }
169:
170: 171: 172: 173: 174:
175: public function check()
176: {
177: $now = time();
178: if (is_null($this->sessionTTL) || $this->sessionTTL == -1)
179: return true;
180: elseif ($_SESSION['ttl'] - $now > 0)
181: {
182: $this->refresh();
183: return true;
184: }
185: else
186: {
187: $this->destroy();
188: return false;
189: }
190: }
191:
192: 193: 194: 195:
196: public function remove($key)
197: {
198: if (isset($_SESSION[$key]))
199: unset($_SESSION[$key]);
200: }
201:
202: 203: 204: 205:
206: public function destroy()
207: {
208: if (isset($_SESSION))
209: {
210: @session_unset();
211: @session_destroy();
212: }
213:
214: if ($this->useCookie)
215: {
216: $cookie = session_get_cookie_params();
217: $cookie['httponly'] = isset($cookie['httponly']) ? $cookie['httponly'] : false;
218: setcookie($this->sessionName, '', time() - 42000, $cookie['path'], $cookie['domain'], $cookie['domain'], $cookie['httponly']);
219: }
220: }
221:
222: }
223:
224: ?>