- Timestamp:
- 11/14/04 10:06:02 (4 years ago)
- Files:
-
- trunk/examples/example_basic_count.php (modified) (2 diffs)
- trunk/examples/example_basic_destroy.php (modified) (1 diff)
- trunk/examples/example_test_null.php (modified) (1 diff)
- trunk/examples/index_sections.php (modified) (1 diff)
- trunk/patSession/Storage.php (modified) (17 diffs)
- trunk/patSession/Storage/Cli.php (modified) (5 diffs)
- trunk/patSession/Storage/Native.php (modified) (4 diffs)
- trunk/patSession/Storage/Null.php (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/examples/example_basic_count.php
r25 r31 24 24 $sess =& patSession::singleton( 'ham' ); 25 25 26 if( isset( $_REQUEST['restart'] ) && $_REQUEST['restart'] == 'force' ) 27 { 28 $sess->destroy(); 29 $sess->restart(); 30 } 31 26 32 // see wheter the started session is new 27 33 if( $sess->isNew() ) … … 41 47 $queryString = $sess->getQueryString(); 42 48 echo '- <a href="' . $_SERVER['PHP_SELF'] . '?' . $queryString . '" title="'. $_SERVER['PHP_SELF'] . '?' . $queryString .'">Continue this session</a> <br />'; 43 echo '- <a href="' . $_SERVER['PHP_SELF'] . '" title="'. $_SERVER['PHP_SELF'] . '">Start over</a> <br />'; 49 echo '- <a href="' . $_SERVER['PHP_SELF'] . '" title="'. $_SERVER['PHP_SELF'] . '">Start over</a> (make sure, that you don\'t send cookies!) <br />'; 50 echo '- <a href="' . $_SERVER['PHP_SELF'] . '?restart=force" title="'. $_SERVER['PHP_SELF'] . '">Restart with cookies</a> (This will destroy the session as well!)<br />'; 44 51 ?> trunk/examples/example_basic_destroy.php
r10 r31 34 34 35 35 echo '...try to recieve a value from the session<br />'; 36 $sess->get( 'fb' , 'frequence modulation');36 $sess->get( 'fb' ); 37 37 ?> trunk/examples/example_test_null.php
r22 r31 22 22 23 23 // most easy way to create a session object 24 $sess =& patSession::singleton( ' ham', 'Null' );24 $sess =& patSession::singleton( 'money', 'Null' ); 25 25 26 echo "Whatever you save, it will get lost... :-)<br />\n";26 echo "Whatever you save, it will not survive the next request!<br />\n"; 27 27 28 echo "Try to save 50 Euro... <br /> \n"; 29 $sess->set( 'save', '50,-- Euro' ); 30 echo "See what was saved: "; 31 var_dump( $sess->get( 'save' ) ); 32 echo "<br />\n"; 28 if( isset( $_REQUEST['continue'] ) && $_REQUEST['continue'] == 'try' ) 29 { 30 echo "See what was saved in the European bank account: "; 31 var_dump( $sess->get( 'account_euro' ) ); 32 echo "<br />\n"; 33 34 echo "See what was saved in the Australian bank account: "; 35 var_dump( $sess->get( 'account_dollar' ) ); 36 echo "<br />\n"; 37 } 38 else 39 { 40 echo "Try to save 50 Euro in our European bank account... <br /> \n"; 41 $sess->set( 'account_euro', '50,-- Euro' ); 42 echo "Try to save 50 Dollar in our Australian bank account... <br /> \n"; 43 $sess->set( 'account_dollar', '$50' ); 44 } 33 45 34 echo "Try to save 50 Dollar... <br /> \n"; 35 $sess->set( 'save', '$50' ); 36 echo "See what was saved: "; 37 var_dump( $sess->get( 'save' ) ); 38 echo "<br />\n"; 46 $queryString = $sess->getQueryString(); 47 echo '- <a href="' . $_SERVER['PHP_SELF'] . '?' . $queryString . '&continue=try" title="'. $_SERVER['PHP_SELF'] . '?' . $queryString .'&continue=try">Continue this session - or at least try to!</a> <br />'; 39 48 ?> trunk/examples/index_sections.php
r29 r31 46 46 'destroy' => array( 47 47 'title' => 'Destroy sessions', 48 'descr' => 'Shows how to terminate a session.' 48 'descr' => 'Shows how to terminate a session. Destroying a session also means to flush all session values.' 49 ), 50 'restart' => array( 51 'title' => 'Restart', 52 'descr' => 'Inactive session may restarted - of course, this will not restore the previous session values. ' 53 . 'Restarting a session means that you start over with an empty session container - but at least, the session will be active again!' 49 54 ), 50 55 'count' => array( trunk/patSession/Storage.php
r29 r31 42 42 { 43 43 /** 44 * session variables 45 * @var mixed $_sess temporary storage container 46 */ 47 var $_sess = null; 48 49 /** 50 * session attributes 51 * @var array $_sessAttributes 52 */ 53 var $_sessAttributes = array(); 54 55 /** 56 * id string 57 * @var mixed $_id 58 */ 59 var $_id = null; 60 61 /** 44 62 * internal state 45 63 * @access protected … … 100 118 101 119 /** 120 * Start the session - interface for PHP4 121 * 122 * @final 123 * @access protected 124 * @param string $id name-prefix used for internal storage of session-data 125 * @param array $options additional session paramter and configuration values 126 * @see __construct() 127 */ 128 function patSession_Storage( $id = 'session', $options = array() ) 129 { 130 $this->__construct( $id, $options ); 131 } 132 133 /** 134 * Start the session 135 * 136 * - pass the configuration options 137 * - start session 138 * - perform security checks 139 * - increase session counter 140 * 141 * @final 142 * @access protected 143 * @param string $id name-prefix used for internal storage of session-data 144 * @param array $options additional session paramter and configuration values 145 */ 146 function __construct( $id = 'session', $options = array() ) 147 { 148 $this->_id = $id; 149 $this->_setOptions( $options ); 150 151 $this->_start(); 152 153 // init shutdown function on PHP 4 154 list( $version ) = explode( '.', phpversion() ); 155 if( $version < 5 ) 156 { 157 register_shutdown_function( array( $this, '_suspend' ) ); 158 } 159 160 $this->_state = 'active'; 161 162 // perform security checks 163 if( !$this->_checkSecurity() ) 164 { 165 $this->sess = array(); 166 } 167 168 $this->_initCounter(); 169 } 170 171 /** 172 * make sure to suspend session values on shutdown 173 * 174 * @access public 175 */ 176 function __destruct() 177 { 178 $this->_suspend(); 179 } 180 181 /** 102 182 * get current state of sessoin 103 183 * … … 128 208 } 129 209 130 return implode( '& ', $query );210 return implode( '&', $query ); 131 211 } 132 212 … … 134 214 * recieve name of this session 135 215 * 136 * @ abstract216 * @final 137 217 * @access private 138 218 * @return string $name session name … … 140 220 function getName() 141 221 { 142 return null;222 return $this->_sessAttributes['name']; 143 223 } 144 224 … … 146 226 * recieve id of this session 147 227 * 148 * @ abstract228 * @final 149 229 * @access private 150 230 * @return string $id session id … … 152 232 function getId() 153 233 { 154 return null; 234 if( $this->_state === 'destroyed' ) 235 { 236 patErrorManager::raiseNotice( 'patSession:' . PATSESSION_ERROR_NOT_ACTIVE, 237 'Session is destroyed - no session id available', 238 'The session was destroyed before and the session id removed.' 239 ); 240 return null; 241 } 242 243 return $this->_sessAttributes['id']; 155 244 } 156 245 … … 158 247 * save data into session 159 248 * 160 * @ abstract249 * @final 161 250 * @access public 162 251 * @param string $name name of variable … … 166 255 function set( $name, $value ) 167 256 { 257 if( $this->_state !== 'active' ) 258 { 259 return patErrorManager::raiseError( 'patSession:' . PATSESSION_ERROR_NOT_ACTIVE, 260 'Cannot set value because session not active.', 261 'Either the session was destroyed, has expired or locked by security' 262 ); 263 } 264 265 $this->_sess[$name] = $value; 168 266 return true; 169 267 } … … 172 270 * get data from session 173 271 * 174 * @ abstract272 * @final 175 273 * @access public 176 274 * @param string $name name of variable … … 179 277 function get( $name ) 180 278 { 279 if( $this->_state !== 'active' ) 280 { 281 return patErrorManager::raiseWarning( 'patSession:' . PATSESSION_ERROR_NOT_ACTIVE, 282 'Cannot recieve value because session not active.', 283 'Either the session was destroyed, has expired or locked because of security reasons.' 284 ); 285 } 286 287 if( isset( $this->_sess[$name] ) ) 288 { 289 return $this->_sess[$name]; 290 } 291 181 292 return null; 182 293 } … … 185 296 * check wheter a session value exists 186 297 * 187 * @ abstract298 * @final 188 299 * @access public 189 300 * @param string $name name of variable … … 192 303 function has( $name ) 193 304 { 194 return null; 305 if( $this->_state !== 'active' ) 306 { 307 return patErrorManager::raiseWarning( 'patSession:' . PATSESSION_ERROR_NOT_ACTIVE, 308 'Cannot recieve value because session not active.', 309 'Either the session was destroyed, has expired or locked because of security reasons.' 310 ); 311 } 312 313 return isset( $this->_sess[$name] ); 195 314 } 196 315 … … 198 317 * unset data from session 199 318 * 200 * @ abstract319 * @final 201 320 * @access public 202 321 * @param string $name name of variable … … 205 324 function clear( $name ) 206 325 { 207 return null; 326 if( $this->_state !== 'active' ) 327 { 328 patErrorManager::raiseNotice( 'patSession:' . PATSESSION_ERROR_NOT_ACTIVE, 329 'Session is not active - nothing to clear', 330 'Either the session was destroyed, has expired or locked because of security reasons.' 331 ); 332 return null; 333 } 334 335 $value = null; 336 if( isset( $this->_sess[$name] ) ) 337 { 338 $value = $this->_sess[$name]; 339 unset( $this->_sess[$name] ); 340 } 341 342 return $value; 208 343 } 209 344 … … 217 352 function destroy() 218 353 { 354 if( $this->_state === 'destroyed' ) 355 { 356 patErrorManager::raiseNotice( 'patSession:' . PATSESSION_ERROR_NOT_ACTIVE, 357 'Session is not active.', 358 'The session was destroyed before.' 359 ); 360 return false; 361 } 362 363 // call event handler 364 if( is_callable( array( $this, '_handleDestroy' ) ) ) 365 { 366 $this->_handleDestroy(); 367 } 368 369 $this->_sess = null; 370 $this->_state = 'destroyed'; 219 371 return true; 220 372 } 221 373 222 374 /** 375 * restart a destroyed or locked sessionb 376 * 377 * @final 378 * @access public 379 * @return boolean $result true on success 380 * @see destroy 381 */ 382 function restart() 383 { 384 if( $this->_state === 'active' ) 385 { 386 return false; 387 } 388 389 $this->_start(); 390 $this->_state = 'active'; 391 392 // perform security checks 393 if( !$this->_checkSecurity( true ) ) 394 { 395 $this->sess = array(); 396 } 397 398 $this->_initCounter(); 399 return true; 400 } 401 402 403 /** 223 404 * create a new session and copy variables from the old one 224 405 * … … 229 410 function fork() 230 411 { 412 if( $this->_state !== 'active' ) 413 { 414 if( !$this->_checkSecurity( true ) ) 415 { 416 return patErrorManager::raiseNotice( 'patSession:' . PATSESSION_ERROR_NOT_ACTIVE, 417 'Session is not active.', 418 'Either the session was destroyed, has expired or locked because of security reasons: '. $this->_state.'.' 419 ); 420 } 421 } 422 423 // call event handler 424 if( is_callable( array( $this, '_handleFork' ) ) ) 425 { 426 $this->_handleFork(); 427 } 428 231 429 return true; 232 430 } trunk/patSession/Storage/Cli.php
r27 r31 7 7 * $Id$ 8 8 * 9 * @version 1.0. 59 * @version 1.0.6 10 10 * @package patSession 11 11 * @subpackage Storage … … 39 39 { 40 40 /** 41 * session variables container42 * @var array $_sess43 */44 var $_sess = null;45 46 /**47 * id string48 * @var string $_id49 */50 var $_id = '42';51 52 /**53 * session name54 * @var string $_id55 */56 var $_name = 'patSession_Cli';57 58 /**59 * constructor for PHP460 *61 * @param string $id name-prefix used for internal storage of session-data62 * @param array $options additional session paramter and configuration values63 * @see __construct64 */65 function patSession_Storage_Cli( $id = 'session', $options = array() )66 {67 $this->__construct( $id, $options );68 }69 70 /**71 41 * Start the session 72 *73 42 * 74 43 * @param string $id name-prefix used for internal storage of session-data 75 44 * @param array $options additional session paramter and configuration values 76 45 */ 77 function _ _construct( $id = 'session', $options = array())46 function _start( ) 78 47 { 79 48 // always start a new session 80 49 $this->_sess = array(); 81 $this->_state = 'active'; 82 $this->_initCounter(); 50 51 if( !isset( $this->_sessAttributes['id'] ) ) 52 { 53 $this->_sessAttributes['id'] = 42; 54 } 55 56 if( !isset( $this->_sessAttributes['name'] ) ) 57 { 58 $this->_sessAttributes['name'] = 'patSession_Cli'; 59 } 83 60 84 61 // check whether the script runs as CLI processor … … 90 67 ); 91 68 } 92 }93 94 /**95 * recieve name of this session96 *97 * @access private98 * @return string $name session name99 */100 function getName()101 {102 return $this->_name;103 }104 105 /**106 * recieve id of this session107 *108 * @access private109 * @return string $id session id110 */111 function getId()112 {113 if( $this->_state === 'destroyed' )114 {115 patErrorManager::raiseNotice( 'patSession:' . PATSESSION_ERROR_NOT_ACTIVE,116 'Session is destroyed - no session id available',117 'The session was destroyed before and the session id removed.'118 );119 return null;120 }121 122 return $this->_id;123 }124 125 /**126 * save data into session127 *128 * @access public129 * @param string $name name of variable130 * @param mixed $value any value to be stored into session131 * @return boolean $result true on success132 */133 function set( $name, $value )134 {135 if( $this->_state !== 'active' )136 {137 return patErrorManager::raiseWarning( 'patSession:' . PATSESSION_ERROR_NOT_ACTIVE,138 'Cannot recieve value because session not active.',139 'Either the session was destroyed, has expired or locked because of security reasons.'140 );141 }142 69 143 $this->_sess[$name] = $value;144 70 return true; 145 71 } 146 72 147 73 /** 148 * get data from session 149 * 150 * @access public 151 * @param string $name name of variable 152 * @return mixed $value the value from session or NULL if not set 153 */ 154 function get( $name ) 155 { 156 if( $this->_state !== 'active' ) 157 { 158 return patErrorManager::raiseWarning( 'patSession:' . PATSESSION_ERROR_NOT_ACTIVE, 159 'Cannot recieve value because session not active.', 160 'Either the session was destroyed, has expired or locked because of security reasons.' 161 ); 162 } 163 164 if( isset( $this->_sess[$name] ) ) 165 { 166 return $this->_sess[$name]; 167 } 168 169 return null; 170 } 171 172 /** 173 * check wheter a session value exists 174 * 175 * @access public 176 * @param string $name name of variable 177 * @return boolean $result true if the variable exists 178 */ 179 function has( $name ) 180 { 181 if( $this->_state !== 'active' ) 182 { 183 return patErrorManager::raiseWarning( 'patSession:' . PATSESSION_ERROR_NOT_ACTIVE, 184 'Cannot recieve value because session not active.', 185 'Either the session was destroyed, has expired or locked because of security reasons.' 186 ); 187 } 188 189 return isset( $this->_sess[$name] ); 190 } 74 * suspend the session values 75 * 76 * Do nothing! In other words, don't make session values persistent 77 * 78 * @access private 79 * @return boolean $result true on success 80 */ 81 function _suspend() 82 { 83 return true; 84 } 191 85 192 /**193 * unset data from session194 *195 * @access public196 * @param string $name name of variable197 * @return mixed $value the value from session or NULL if not set198 */199 function clear( $name )200 {201 if( $this->_state !== 'active' )202 {203 patErrorManager::raiseNotice( 'patSession:' . PATSESSION_ERROR_NOT_ACTIVE,204 'Session is not active - nothing to clear',205 'Either the session was destroyed, has expired or locked because of security reasons.'206 );207 return null;208 }209 210 $value = null;211 if( isset( $this->_sess[$name] ) )212 {213 $value = $this->_sess[$name];214 unset( $this->_sess[$name] );215 }216 217 return $value;218 }219 220 /**221 * unset session variables and destroy session222 *223 * @access public224 * @return boolean $result true on success225 */226 function destroy()227 {228 if( $this->_state === 'destroyed' )229 {230 patErrorManager::raiseNotice( 'patSession:' . PATSESSION_ERROR_NOT_ACTIVE,231 'Session is not active.',232 'The session was destroyed before.'233 );234 return false;235 }236 237 $this->_sess = array();238 $this->_state = 'destroyed';239 return true;240 }241 242 /**243 * create a new session and copy variables from the old one244 *245 * @access public246 * @return boolean $result true on success247 * @todo think about storage of session values - what happens to $_SESSION248 */249 function fork()250 {251 if( $this->_state !== 'active' )252 {253 if( !$this->_checkSecurity( true ) )254 {255 return patErrorManager::raiseNotice( 'patSession:' . PATSESSION_ERROR_NOT_ACTIVE,256 'Session is not active.',257 'Either the session was destroyed, has expired or locked because of security reasons: '. $this->_state.'.'258 );259 }260 }261 262 return true;263 }264 265 86 /** 266 87 * set additional session options … … 277 98 if( isset( $options['name'] ) ) 278 99 { 279 $this->_ name= $options['name'];100 $this->_sessAttributes['name'] = $options['name']; 280 101 } 281 102 … … 283 104 if( isset( $options['id'] ) ) 284 105 { 285 $this->_ id= $options['id'];106 $this->_sessAttributes['id'] = $options['id']; 286 107 } 287 108 return true; trunk/patSession/Storage/Native.php
r27 r31 8 8 * $Id$ 9 9 * 10 * @version 1.0. 510 * @version 1.0.6 11 11 * @package patSession 12 12 * @subpackage Storage … … 29 29 { 30 30 /** 31 * session variables 32 * @var array $_sess reference to the session-storage 31 * start session 32 * 33 * - start session (if not already running) 34 * - select storage container by id 35 * - store id in attributes 36 * - store name in attributes 37 * 38 * @access private 39 * @return boolean $result true on success 33 40 */ 34 var $_sess = null; 35 36 /** 37 * id string 38 * @var string $_id 39 */ 40 var $_id = null; 41 42 /** 43 * constructor for PHP4 44 * 45 * @param string $id name-prefix used for internal storage of session-data 46 * @param array $options additional session paramter and configuration values 47 * @see __construct 48 */ 49 function patSession_Storage_Native( $id = 'session', $options = array() ) 50 { 51 $this->__construct( $id, $options ); 52 } 53 54 /** 55 * Start the session 56 * 57 * - pass the configuration options 58 * - start php-session (if not running) 59 * - select the the sorage container by id 60 * - perform security checks 61 * - increase session counter 62 * 63 * @param string $id name-prefix used for internal storage of session-data 64 * @param array $options additional session paramter and configuration values 65 */ 66 function __construct( $id = 'session', $options = array() ) 67 { 68 $this->_id = $id; 69 $this->_setOptions( $options ); 70 41 function _start() 42 { 71 43 // start session if not startet 72 44 if( !defined( 'SID' ) ) … … 82 54 83 55 $this->_sess =& $_SESSION[$this->_id]; 84 $this->_state = 'active';85 86 // perform security checks87 if( !$this->_checkSecurity() )88 {89 $this->sess = array();90 }91 56 92 $this->_initCounter(); 93 } 94 95 /** 96 * recieve name of this session 97 * 98 * @access private 99 * @return string $name session name 100 */ 101 function getName() 102 { 103 return session_name(); 57 $this->_sessAttributes['id'] = session_id(); 58 $this->_sessAttributes['name'] = session_name(); 59 60 return TRUE; 104 61 } 105 62 106 63 /** 107 * recieve id of this session 64 * suspend the session values 65 * 66 * Make session values persistent 108 67 * 109 68 * @access private 110 * @return string $id session id69 * @return boolean $result true on success 111 70 */ 112 function getId()71 function _suspend() 113 72 { 114 if( $this->_state === 'destroyed' ) 115 { 116 patErrorManager::raiseNotice( 'patSession:' . PATSESSION_ERROR_NOT_ACTIVE, 117 'Session is destroyed - no session id available', 118 'The session was destroyed before and the session id removed.' 119 ); 120 return null; 121 } 122 123 return session_id(); 73 // PHP does the trick automatically 74 return true; 124 75 } 125 76 126 77 /** 127 * save data intosession78 * event handler to destroy a the session 128 79 * 129 * @access public 130 * @param string $name name of variable 131 * @param mixed $value any value to be stored into session 80 * @access private 132 81 * @return boolean $result true on success 133 82 */ 134 function set( $name, $value)83 function _handleDestroy() 135 84 { 136 if( $this->_state !== 'active' )137 {138 return patErrorManager::raiseError( 'patSession:' . PATSESSION_ERROR_NOT_ACTIVE,139 'Cannot set value because session not active.',140 'Either the session was destroyed, has expired or locked by security'141 );142 }143 144 $this->_sess[$name] = $value;145 return true;146 }147 148 /**149 * get data from session150 *151 * @access public152 * @param string $name name of variable153 * @return mixed $value the value from session or NULL if not set154 */155 function get( $name )156 {157 if( $this->_state !== 'active' )158 {159 return patErrorManager::raiseWarning( 'patSession:' . PATSESSION_ERROR_NOT_ACTIVE,160 'Cannot recieve value because session not active.',161 'Either the session was destroyed, has expired or locked because of security reasons.'162 );163 }164 165 if( isset( $this->_sess[$name] ) )166 {167 return $this->_sess[$name];168 }169 170 return null;171 }172 173 /**174 * check wheter a session value exists175 *176 * @access public177
