Class THttpSession

Description

Implements interfaces:

  • IteratorAggregate (internal interface)
  • ArrayAccess (internal interface)
  • Countable (internal interface)
  • IModule

THttpSession class

THttpSession provides session-level data management and the related configurations. To start the session, call open; to complete and send out session data, call close; to destroy the session, call destroy. If AutoStart is true, then the session will be started once the session module is loaded and initialized.

To access data stored in session, use THttpSession like an associative array. For example,

  1.    $session=new THttpSession;
  2.    $session->open();
  3.    $value1=$session['name1'];  // get session variable 'name1'
  4.    $value2=$session['name2'];  // get session variable 'name2'
  5.    foreach($session as $name=>$value// traverse all session variables
  6.    $session['name3']=$value3;  // set session variable 'name3'

The following configurations are available for session: AutoStart, CookieMode, SavePath, UseCustomStorage, GCProbability, Timeout. See the corresponding setter and getter documentation for more information. Note, these properties must be set before the session is started.

THttpSession can be inherited with customized session storage method. Override _open, _close, _read, _write, _destroy and _gc and set UseCustomStorage to true. Then, the session data will be stored using the above methods.

By default, THttpSession is registered with TApplication as the request module. It can be accessed via TApplication::getSession().

THttpSession may be configured in application configuration file as follows,

  1.  <module id="session" class="THttpSession" SessionName="SSID" SavePath="/tmp"
  2.          CookieMode="Allow" UseCustomStorage="false" AutoStart="true" GCProbability="1"
  3.          UseTransparentSessionID="true" TimeOut="3600" />
where SessionName, SavePath, CookieMode, UseCustomStorage, AutoStart, GCProbability, UseTransparentSessionID and TimeOut are configurable properties of THttpSession.

  • author: Qiang Xue <qiang.xue@gmail.com>
  • version: $Id: THttpSession.php 3114 2012-02-28 09:52:47Z GODZilla0480@gmail.com $
  • since: 3.0

Located in /Web/THttpSession.php (line 64)

TComponent
   |
   --TApplicationComponent
      |
      --THttpSession
Direct descendents
Class Description
TCacheHttpSession TCacheHttpSession class
Method Summary
void add (mixed $key, mixed $value)
void clear ()
void close ()
boolean contains (mixed $key)
integer count ()
void destroy ()
boolean getAutoStart ()
integer getCount ()
integer getGCProbability ()
string getID ()
boolean getIsStarted ()
array getKeys ()
string getSavePath ()
string getSessionID ()
string getSessionName ()
integer getTimeout ()
boolean getUseCustomStorage ()
void init (TXmlElement $config)
mixed itemAt (mixed $key)
boolean offsetExists (mixed $offset)
mixed offsetGet (integer $offset)
void offsetSet (integer $offset, mixed $item)
void offsetUnset (mixed $offset)
void open ()
string regenerate ([boolean $deleteOld = false])
mixed remove (mixed $key)
void setAutoStart (boolean $value)
void setGCProbability (integer $value)
void setID (string $value)
void setSavePath (string $value)
void setSessionID (string $value)
void setSessionName (string $value)
void setTimeout (integer $value)
void setUseCustomStorage (boolean $value)
void setUseTransparentSessionID (boolean $value)
array toArray ()
boolean _close ()
boolean _destroy (string $id)
boolean _gc (integer $maxLifetime)
boolean _open (string $savePath, string $sessionName)
string _read (string $id)
boolean _write (string $id, string $data)
Methods
add (line 534)

Adds a session variable.

Note, if the specified name already exists, the old value will be removed first.

  • access: public
void add (mixed $key, mixed $value)
  • mixed $key: session variable name
  • mixed $value: session variable value
clear (line 559)

Removes all session variables

  • access: public
void clear ()
close (line 142)

Ends the current session and store session data.

  • access: public
void close ()
contains (line 569)
  • return: whether there is the named session variable
  • access: public
boolean contains (mixed $key)
  • mixed $key: session variable name
count (line 504)

Returns the number of items in the session.

This method is required by Countable interface.

  • return: number of items in the session.
  • access: public
integer count ()

Implementation of:
Countable::count
destroy (line 154)

Destroys all data registered to a session.

  • access: public
void destroy ()
getAutoStart (line 321)
  • return: whether the session should be automatically started when the session module is initialized, defaults to false.
  • access: public
boolean getAutoStart ()
getCookie (line 271)
  • return: cookie that will be used to store session ID
  • access: public
THttpCookie getCookie ()
getCookieMode (line 281)
  • return: how to use cookie to store session ID. Defaults to THttpSessionCookieMode::Allow.
  • access: public
THttpSessionCookieMode getCookieMode ()
getCount (line 494)
  • return: the number of session variables
  • access: public
integer getCount ()
getGCProbability (line 341)
  • return: the probability (percentage) that the gc (garbage collection) process is started on every session initialization, defaults to 1 meaning 1% chance.
  • access: public
integer getGCProbability ()
getID (line 94)
  • return: id of this module
  • access: public
string getID ()
getIsStarted (line 180)
  • return: whether the session has started
  • access: public
boolean getIsStarted ()
getIterator (line 486)

Returns an iterator for traversing the session variables.

This method is required by the interface IteratorAggregate.

  • return: an iterator for traversing the session variables.
  • access: public
TSessionIterator getIterator ()

Implementation of:
IteratorAggregate::getIterator
getKeys (line 512)
  • return: the list of session variable names
  • access: public
array getKeys ()
getSavePath (line 230)
  • return: the current session save path, defaults to '/tmp'.
  • access: public
string getSavePath ()
getSessionID (line 188)
  • return: the current session ID
  • access: public
string getSessionID ()
getSessionName (line 208)
  • return: the current session name
  • access: public
string getSessionName ()
getTimeout (line 395)
  • return: the number of seconds after which data will be seen as 'garbage' and cleaned up, defaults to 1440 seconds.
  • access: public
integer getTimeout ()
getUseCustomStorage (line 252)
  • return: whether to use user-specified handlers to store session data. Defaults to false.
  • access: public
boolean getUseCustomStorage ()
getUseTransparentSessionID (line 371)
  • return: whether transparent sid support is enabled or not, defaults to false.
  • access: public
boolean getUseTransparentSessionID ()
init (line 113)

Initializes the module.

This method is required by IModule. If AutoStart is true, the session will be started.

  • access: public
void init (TXmlElement $config)

Redefined in descendants as:
itemAt (line 523)

Returns the session variable value with the session variable name.

This method is exactly the same as offsetGet.

  • return: the session variable value, null if no such variable exists
  • access: public
mixed itemAt (mixed $key)
  • mixed $key: the session variable name
offsetExists (line 587)

This method is required by the interface ArrayAccess.

  • access: public
boolean offsetExists (mixed $offset)
  • mixed $offset: the offset to check on

Implementation of:
ArrayAccess::offsetExists
offsetGet (line 597)

This method is required by the interface ArrayAccess.

  • return: the element at the offset, null if no element is found at the offset
  • access: public
mixed offsetGet (integer $offset)
  • integer $offset: the offset to retrieve element.

Implementation of:
ArrayAccess::offsetGet
offsetSet (line 607)

This method is required by the interface ArrayAccess.

  • access: public
void offsetSet (integer $offset, mixed $item)
  • integer $offset: the offset to set element
  • mixed $item: the element value

Implementation of:
ArrayAccess::offsetSet
offsetUnset (line 616)

This method is required by the interface ArrayAccess.

  • access: public
void offsetUnset (mixed $offset)
  • mixed $offset: the offset to unset element

Implementation of:
ArrayAccess::offsetUnset
open (line 125)

Starts the session if it has not started yet.

  • access: public
void open ()
regenerate (line 170)

Update the current session id with a newly generated one

string regenerate ([boolean $deleteOld = false])
  • boolean $deleteOld: Whether to delete the old associated session or not.
remove (line 544)

Removes a session variable.

  • return: the removed value, null if no such session variable.
  • access: public
mixed remove (mixed $key)
  • mixed $key: the name of the session variable to be removed
setAutoStart (line 330)
  • throws: TInvalidOperationException if session is started already
  • access: public
void setAutoStart (boolean $value)
  • boolean $value: whether the session should be automatically started when the session module is initialized, defaults to false.
setCookieMode (line 295)
  • throws: TInvalidOperationException if session is started already
  • access: public
void setCookieMode (THttpSessionCookieMode $value)
setGCProbability (line 351)
  • throws: TInvalidDataValueException if the value is beyond [0,100].
  • throws: TInvalidOperationException if session is started already
  • access: public
void setGCProbability (integer $value)
  • integer $value: the probability (percentage) that the gc (garbage collection) process is started on every session initialization.
setID (line 102)
  • access: public
void setID (string $value)
  • string $value: id of this module
setSavePath (line 239)
  • throws: TInvalidOperationException if session is started already
  • access: public
void setSavePath (string $value)
  • string $value: the current session save path
setSessionID (line 197)
  • throws: TInvalidOperationException if session is started already
  • access: public
void setSessionID (string $value)
  • string $value: the session ID for the current session
setSessionName (line 217)
  • throws: TInvalidOperationException if session is started already
  • access: public
void setSessionName (string $value)
  • string $value: the session name for the current session, must be an alphanumeric string, defaults to PHPSESSID
setTimeout (line 404)
  • throws: TInvalidOperationException if session is started already
  • access: public
void setTimeout (integer $value)
  • integer $value: the number of seconds after which data will be seen as 'garbage' and cleaned up
setUseCustomStorage (line 263)
  • access: public
void setUseCustomStorage (boolean $value)
  • boolean $value: whether to use user-specified handlers to store session data. If true, make sure the methods _open, _close, _read, _write, _destroy, and _gc are overridden in child class, because they will be used as the callback handlers.
setUseTransparentSessionID (line 379)
  • access: public
void setUseTransparentSessionID (boolean $value)
  • boolean $value: whether transparent sid support is enabled or not.
toArray (line 577)
  • return: the list of all session variables in array
  • access: public
array toArray ()
_close (line 429)

Session close handler.

This method should be overridden if UseCustomStorage is set true.

  • return: whether session is closed successfully
  • access: public
boolean _close ()
_destroy (line 463)

Session destroy handler.

This method should be overridden if UseCustomStorage is set true.

  • return: whether session is destroyed successfully
  • access: public
boolean _destroy (string $id)
  • string $id: session ID

Redefined in descendants as:
_gc (line 474)

Session GC (garbage collection) handler.

This method should be overridden if UseCustomStorage is set true.

  • return: whether session is GCed successfully
  • access: public
boolean _gc (integer $maxLifetime)
  • integer $maxLifetime: the number of seconds after which data will be seen as 'garbage' and cleaned up.
_open (line 419)

Session open handler.

This method should be overridden if UseCustomStorage is set true.

  • return: whether session is opened successfully
  • access: public
boolean _open (string $savePath, string $sessionName)
  • string $savePath: session save path
  • string $sessionName: session name
_read (line 440)

Session read handler.

This method should be overridden if UseCustomStorage is set true.

  • return: the session data
  • access: public
string _read (string $id)
  • string $id: session ID

Redefined in descendants as:
_write (line 452)

Session write handler.

This method should be overridden if UseCustomStorage is set true.

  • return: whether session write is successful
  • access: public
boolean _write (string $id, string $data)
  • string $id: session ID
  • string $data: session data

Redefined in descendants as:

Inherited Methods

Inherited From TApplicationComponent

TApplicationComponent::getApplication()
TApplicationComponent::getRequest()
TApplicationComponent::getResponse()
TApplicationComponent::getService()
TApplicationComponent::getSession()
TApplicationComponent::getUser()
TApplicationComponent::publishAsset()
TApplicationComponent::publishFilePath()

Inherited From TComponent

TComponent::addParsedObject()
TComponent::attachEventHandler()
TComponent::canGetProperty()
TComponent::canSetProperty()
TComponent::createdOnTemplate()
TComponent::detachEventHandler()
TComponent::evaluateExpression()
TComponent::evaluateStatements()
TComponent::getEventHandlers()
TComponent::getSubProperty()
TComponent::hasEvent()
TComponent::hasEventHandler()
TComponent::hasProperty()
TComponent::raiseEvent()
TComponent::setSubProperty()
TComponent::__call()
TComponent::__get()
TComponent::__set()
TComponent::__sleep()
TComponent::__wakeup()

Documentation generated on Mon, 25 Jun 2012 14:38:24 +0200 by phpDocumentor 1.4.3