Versions
Look up a class, method, property or event

CHttpSession

Package system.web
Inheritance class CHttpSession » CApplicationComponent » CComponent
Implements IApplicationComponent, IteratorAggregate, Traversable, ArrayAccess, Countable
Subclasses CCacheHttpSession, CDbHttpSession
Since 1.0
Version $Id: CHttpSession.php 1446 2009-10-14 19:54:48Z qiang.xue $
CHttpSession 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 set true, the session will be started automatically when the application component is initialized by the application.

CHttpSession can be used like an array to set and get session data. For example,
  $session=new CHttpSession;
  $session->open();
  $value1=$session['name1'];  // get session variable 'name1'
  $value2=$session['name2'];  // get session variable 'name2'
  foreach($session as $name=>$value) // traverse all session variables
  $session['name3']=$value3;  // set session variable 'name3'


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

CHttpSession can be extended to support customized session storage. Override openSession, closeSession, readSession, writeSession, destroySession and gcSession and set useCustomStorage to true. Then, the session data will be stored and retrieved using the above methods.

CHttpSession is a Web application component that can be accessed via CWebApplication::getSession().

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
autoStart boolean whether the session should be automatically started when the session application component is initialized, defaults to true. CHttpSession
behaviors array the behaviors that should be attached to this component. CApplicationComponent
cookieMode string how to use cookie to store session ID. CHttpSession
cookieParams array the session cookie parameters. CHttpSession
count integer the number of session variables CHttpSession
gCProbability integer the probability (percentage) that the gc (garbage collection) process is started on every session initialization, defaults to 1 meaning 1% chance. CHttpSession
isInitialized boolean whether this application component has been initialized (i. CApplicationComponent
isStarted boolean whether the session has started CHttpSession
iterator CHttpSessionIterator Returns an iterator for traversing the session variables. CHttpSession
keys array the list of session variable names CHttpSession
savePath string the current session save path, defaults to '/tmp'. CHttpSession
sessionID string the current session ID CHttpSession
sessionName string the current session name CHttpSession
timeout integer the number of seconds after which data will be seen as 'garbage' and cleaned up, defaults to 1440 seconds. CHttpSession
useCustomStorage boolean Returns a value indicating whether to use custom session storage. CHttpSession
useTransparentSessionID boolean whether transparent sid support is enabled or not, defaults to false. CHttpSession

Public Methods

Hide inherited methods

MethodDescriptionDefined By
__call() Calls the named method which is not a class method. CComponent
__get() Returns a property value, an event handler list or a behavior based on its name. CComponent
__isset() Checks if a property value is null. CComponent
__set() Sets value of a component property. CComponent
__unset() Sets a component property to be null. CComponent
add() Adds a session variable. CHttpSession
asa() Returns the named behavior object. CComponent
attachBehavior() Attaches a behavior to this component. CComponent
attachBehaviors() Attaches a list of behaviors to the component. CComponent
attachEventHandler() Attaches an event handler to an event. CComponent
canGetProperty() Determines whether a property can be read. CComponent
canSetProperty() Determines whether a property can be set. CComponent
clear() Removes all session variables CHttpSession
close() Ends the current session and store session data. CHttpSession
closeSession() Session close handler. CHttpSession
contains() CHttpSession
count() Returns the number of items in the session. CHttpSession
destroy() Frees all session variables and destroys all data registered to a session. CHttpSession
destroySession() Session destroy handler. CHttpSession
detachBehavior() Detaches a behavior from the component. CComponent
detachBehaviors() Detaches all behaviors from the component. CComponent
detachEventHandler() Detaches an existing event handler. CComponent
disableBehavior() Disables an attached behavior. CComponent
disableBehaviors() Disables all behaviors attached to this component. CComponent
enableBehavior() Enables an attached behavior. CComponent
enableBehaviors() Enables all behaviors attached to this component. CComponent
gcSession() Session GC (garbage collection) handler. CHttpSession
getCookieMode() CHttpSession
getCookieParams() CHttpSession
getCount() CHttpSession
getEventHandlers() Returns the list of attached event handlers for an event. CComponent
getGCProbability() CHttpSession
getIsInitialized() CApplicationComponent
getIsStarted() CHttpSession
getIterator() Returns an iterator for traversing the session variables. CHttpSession
getKeys() CHttpSession
getSavePath() CHttpSession
getSessionID() CHttpSession
getSessionName() CHttpSession
getTimeout() CHttpSession
getUseCustomStorage() Returns a value indicating whether to use custom session storage. CHttpSession
getUseTransparentSessionID() CHttpSession
hasEvent() Determines whether an event is defined. CComponent
hasEventHandler() Checks whether the named event has attached handlers. CComponent
hasProperty() Determines whether a property is defined. CComponent
init() Initializes the application component. CHttpSession
itemAt() Returns the session variable value with the session variable name. CHttpSession
offsetExists() This method is required by the interface ArrayAccess. CHttpSession
offsetGet() This method is required by the interface ArrayAccess. CHttpSession
offsetSet() This method is required by the interface ArrayAccess. CHttpSession
offsetUnset() This method is required by the interface ArrayAccess. CHttpSession
open() Starts the session if it has not started yet. CHttpSession
openSession() Session open handler. CHttpSession
raiseEvent() Raises an event. CComponent
readSession() Session read handler. CHttpSession
remove() Removes a session variable. CHttpSession
setCookieMode() CHttpSession
setCookieParams() Sets the session cookie parameters. CHttpSession
setGCProbability() CHttpSession
setSavePath() CHttpSession
setSessionID() CHttpSession
setSessionName() CHttpSession
setTimeout() CHttpSession
setUseTransparentSessionID() CHttpSession
toArray() CHttpSession
writeSession() Session write handler. CHttpSession

Property Details

autoStart property
public boolean $autoStart;

whether the session should be automatically started when the session application component is initialized, defaults to true.

cookieMode property
public string getCookieMode()
public void setCookieMode(string $value)

how to use cookie to store session ID. Defaults to 'Allow'.

cookieParams property
public array getCookieParams()
public void setCookieParams(array $value)

the session cookie parameters.

count property read-only
public integer getCount()

the number of session variables

gCProbability property
public integer getGCProbability()
public void setGCProbability(integer $value)

the probability (percentage) that the gc (garbage collection) process is started on every session initialization, defaults to 1 meaning 1% chance.

isStarted property read-only
public boolean getIsStarted()

whether the session has started

iterator property read-only

Returns an iterator for traversing the session variables. This method is required by the interface IteratorAggregate.

keys property read-only
public array getKeys()

the list of session variable names

savePath property
public string getSavePath()
public void setSavePath(string $value)

the current session save path, defaults to '/tmp'.

sessionID property
public string getSessionID()
public void setSessionID(string $value)

the current session ID

sessionName property
public string getSessionName()
public void setSessionName(string $value)

the current session name

timeout property
public integer getTimeout()
public void setTimeout(integer $value)

the number of seconds after which data will be seen as 'garbage' and cleaned up, defaults to 1440 seconds.

useCustomStorage property read-only
public boolean getUseCustomStorage()

Returns a value indicating whether to use custom session storage. This method should be overriden to return true if custom session storage handler should be used. If returning true, make sure the methods openSession, closeSession, readSession, writeSession, destroySession, and gcSession are overridden in child class, because they will be used as the callback handlers. The default implementation always return false.

useTransparentSessionID property
public boolean getUseTransparentSessionID()
public void setUseTransparentSessionID(boolean $value)

whether transparent sid support is enabled or not, defaults to false.

Method Details

add() method
public void add(mixed $key, mixed $value)
$key mixed session variable name
$value mixed session variable value

Adds a session variable. Note, if the specified name already exists, the old value will be removed first.

clear() method
public void clear()

Removes all session variables

close() method
public void close()

Ends the current session and store session data.

closeSession() method
public boolean closeSession()
{return} boolean whether session is closed successfully

Session close handler. This method should be overridden if useCustomStorage is set true. Do not call this method directly.

contains() method
public boolean contains(mixed $key)
$key mixed session variable name
{return} boolean whether there is the named session variable

count() method
public integer count()
{return} integer number of items in the session.

Returns the number of items in the session. This method is required by Countable interface.

destroy() method
public void destroy()

Frees all session variables and destroys all data registered to a session.

destroySession() method
public boolean destroySession(string $id)
$id string session ID
{return} boolean whether session is destroyed successfully

Session destroy handler. This method should be overridden if useCustomStorage is set true. Do not call this method directly.

gcSession() method
public boolean gcSession(integer $maxLifetime)
$maxLifetime integer the number of seconds after which data will be seen as 'garbage' and cleaned up.
{return} boolean whether session is GCed successfully

Session GC (garbage collection) handler. This method should be overridden if useCustomStorage is set true. Do not call this method directly.

getCookieMode() method
public string getCookieMode()
{return} string how to use cookie to store session ID. Defaults to 'Allow'.

getCookieParams() method
public array getCookieParams()
{return} array the session cookie parameters.

getCount() method
public integer getCount()
{return} integer the number of session variables

getGCProbability() method
public integer getGCProbability()
{return} integer the probability (percentage) that the gc (garbage collection) process is started on every session initialization, defaults to 1 meaning 1% chance.

getIsStarted() method
public boolean getIsStarted()
{return} boolean whether the session has started

getIterator() method
public CHttpSessionIterator getIterator()
{return} CHttpSessionIterator an iterator for traversing the session variables.

Returns an iterator for traversing the session variables. This method is required by the interface IteratorAggregate.

getKeys() method
public array getKeys()
{return} array the list of session variable names

getSavePath() method
public string getSavePath()
{return} string the current session save path, defaults to '/tmp'.

getSessionID() method
public string getSessionID()
{return} string the current session ID

getSessionName() method
public string getSessionName()
{return} string the current session name

getTimeout() method
public integer getTimeout()
{return} integer the number of seconds after which data will be seen as 'garbage' and cleaned up, defaults to 1440 seconds.

getUseCustomStorage() method
public boolean getUseCustomStorage()
{return} boolean whether to use custom storage.

Returns a value indicating whether to use custom session storage. This method should be overriden to return true if custom session storage handler should be used. If returning true, make sure the methods openSession, closeSession, readSession, writeSession, destroySession, and gcSession are overridden in child class, because they will be used as the callback handlers. The default implementation always return false.

getUseTransparentSessionID() method
public boolean getUseTransparentSessionID()
{return} boolean whether transparent sid support is enabled or not, defaults to false.

init() method
public void init()

Initializes the application component. This method is required by IApplicationComponent and is invoked by application.

itemAt() method
public mixed itemAt(mixed $key)
$key mixed the session variable name
{return} mixed the session variable value, null if no such variable exists

Returns the session variable value with the session variable name. This method is exactly the same as offsetGet.

offsetExists() method
public boolean offsetExists(mixed $offset)
$offset mixed the offset to check on
{return} boolean

This method is required by the interface ArrayAccess.

offsetGet() method
public mixed offsetGet(integer $offset)
$offset integer the offset to retrieve element.
{return} mixed the element at the offset, null if no element is found at the offset

This method is required by the interface ArrayAccess.

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

This method is required by the interface ArrayAccess.

offsetUnset() method
public void offsetUnset(mixed $offset)
$offset mixed the offset to unset element

This method is required by the interface ArrayAccess.

open() method
public void open()

Starts the session if it has not started yet.

openSession() method
public boolean openSession(string $savePath, string $sessionName)
$savePath string session save path
$sessionName string session name
{return} boolean whether session is opened successfully

Session open handler. This method should be overridden if useCustomStorage is set true. Do not call this method directly.

readSession() method
public string readSession(string $id)
$id string session ID
{return} string the session data

Session read handler. This method should be overridden if useCustomStorage is set true. Do not call this method directly.

remove() method
public mixed remove(mixed $key)
$key mixed the name of the session variable to be removed
{return} mixed the removed value, null if no such session variable.

Removes a session variable.

setCookieMode() method
public void setCookieMode(string $value)
$value string how to use cookie to store session ID. Valid values include 'none', 'allow' and 'only'.

setCookieParams() method
public void setCookieParams(array $value)
$value array cookie parameters, valid keys include: lifetime, path, domain, secure.

Sets the session cookie parameters. The effect of this method only lasts for the duration of the script. Call this method before the session starts.

setGCProbability() method
public void setGCProbability(integer $value)
$value integer the probability (percentage) that the gc (garbage collection) process is started on every session initialization.

setSavePath() method
public void setSavePath(string $value)
$value string the current session save path

setSessionID() method
public void setSessionID(string $value)
$value string the session ID for the current session

setSessionName() method
public void setSessionName(string $value)
$value string the session name for the current session, must be an alphanumeric string, defaults to PHPSESSID

setTimeout() method
public void setTimeout(integer $value)
$value integer the number of seconds after which data will be seen as 'garbage' and cleaned up

setUseTransparentSessionID() method
public void setUseTransparentSessionID(boolean $value)
$value boolean whether transparent sid support is enabled or not.

toArray() method
public array toArray()
{return} array the list of all session variables in array

writeSession() method
public boolean writeSession(string $id, string $data)
$id string session ID
$data string session data
{return} boolean whether session write is successful

Session write handler. This method should be overridden if useCustomStorage is set true. Do not call this method directly.

Be the first person to leave a comment

Please to leave your comment.