1: <?php
2: 3: 4: 5: 6: 7: 8: 9:
10:
11: class XCube_Session
12: {
13: 14: 15: 16:
17: var $mSessionName = '';
18:
19: 20: 21: 22:
23: var $mSessionLifetime = 0;
24:
25: 26: 27: 28:
29: var $mSetupSessionHandler = null;
30:
31: 32: 33: 34:
35: var $mGetSessionCookiePath = null;
36:
37: function XCube_Session($sessionName='', $sessionExpire=0)
38: {
39: $this->setParam($sessionName, $sessionExpire);
40:
41: $this->mSetupSessionHandler = new XCube_Delegate();
42: $this->mSetupSessionHandler->register('XCube_Session.SetupSessionHandler');
43:
44: $this->mGetSessionCookiePath = new XCube_Delegate();
45: $this->mGetSessionCookiePath->register('XCube_Session.GetSessionCookiePath');
46: }
47:
48: 49: 50:
51: function setParam($sessionName='', $sessionExpire=0)
52: {
53: $allIniArray = ini_get_all();
54:
55: if ($sessionName !='') {
56: $this->mSessionName = $sessionName;
57: } else {
58: $this->mSessionName = $allIniArray['session.name']['global_value'];
59: }
60:
61: if (!empty($sessionExpire)) {
62: $this->mSessionLifetime = 60 * $sessionExpire;
63: } else {
64: $this->mSessionLifetime = $allIniArray['session.cookie_lifetime']['global_value'];
65: }
66: }
67:
68: 69: 70:
71: function start()
72: {
73: $this->mSetupSessionHandler->call();
74:
75: session_name($this->mSessionName);
76: session_set_cookie_params($this->mSessionLifetime, $this->_cookiePath());
77:
78: session_start();
79:
80: if (!empty($this->mSessionLifetime) && isset($_COOKIE[$this->mSessionName])) {
81:
82: setcookie($this->mSessionName, session_id(), time() + $this->mSessionLifetime, $this->_cookiePath());
83: }
84: }
85:
86: 87: 88:
89: function destroy($forceCookieClear = false)
90: {
91:
92:
93:
94: $currentSessionName = session_name();
95: if (isset($_COOKIE[$currentSessionName])) {
96: if ($forceCookieClear || ($currentSessionName != $this->mSessionName)) {
97:
98: setcookie($currentSessionName, '', time() - 86400, $this->_cookiePath());
99: }
100: }
101: session_destroy();
102: }
103:
104: 105: 106:
107: function regenerate()
108: {
109: $oldSessionID = session_id();
110: session_regenerate_id();
111: $newSessionID = session_id();
112: session_id($oldSessionID);
113: $this->destroy();
114: $oldSession = $_SESSION;
115: session_id($newSessionID);
116: $this->start();
117: $_SESSION = array();
118: foreach (array_keys($oldSession) as $key) {
119: $_SESSION[$key] = $oldSession[$key];
120: }
121: }
122:
123: 124: 125:
126: function rename()
127: {
128: if (session_name() != $this->mSessionName) {
129: $oldSessionID = session_id();
130: $oldSession = $_SESSION;
131: $this->destroy();
132: session_id($oldSessionID);
133: $this->start();
134: $_SESSION = array();
135: foreach (array_keys($oldSession) as $key) {
136: $_SESSION[$key] = $oldSession[$key];
137: }
138: }
139: }
140:
141: 142: 143:
144: function _cookiePath()
145: {
146: static $sessionCookiePath = null;
147: if (empty($sessionCookiePath)) {
148: $this->mGetSessionCookiePath->call(new XCube_Ref($sessionCookiePath));
149: if (empty($sessionCookiePath)) {
150: $sessionCookiePath = '/';
151: }
152: }
153: return $sessionCookiePath;
154: }
155: }
156: ?>
157: