class Session implements IteratorAggregate

Class for managing HTTP sessions

Provides access to session-state values as well as session-level settings and lifetime management methods. Based on the standard PHP session handling mechanism it provides more advanced features such as expire timeouts.

Methods

__construct( string $store = 'none', array $options = array(), JSessionHandlerInterface $handlerInterface = null)

Constructor

mixed
__get( string $name)

Magic method to get read-only access to properties.

static  Session
getInstance( string $store, array $options, JSessionHandlerInterface $handlerInterface = null)

Returns the global Session object, only creating it if it doesn't already exist.

string
getState()

Get current state of session

integer
getExpire()

Get expiration time in seconds

string
getToken( boolean $forceNew = false)

Get a session token, if a token isn't set yet one will be generated.

boolean
hasToken( string $tCheck, boolean $forceExpire = true)

Method to determine if a token exists in the session. If not the session will be set to expired

static  string
getFormToken( boolean $forceNew = false)

Method to determine a hash for anti-spoofing variable names

getIterator()

Retrieve an external iterator.

static  boolean
checkToken( string $method = 'post')

Checks for a form token in the request.

string
getName()

Get session name

string
getId()

Get session id

Registry
getData()

Returns a clone of the internal data pointer

static  array
getStores()

Get the session handlers

boolean
isActive()

Shorthand to check if the session is active

boolean
isNew()

Check whether this session is currently created

void
initialise( Input $input, JEventDispatcher $dispatcher = null)

Check whether this session is currently created

mixed
get( string $name, mixed $default = null, string $namespace = 'default')

Get data from the session store

mixed
set( string $name, mixed $value = null, string $namespace = 'default')

Set data into the session store.

boolean
has( string $name, string $namespace = 'default')

Check whether data exists in the session store

mixed
clear( string $name, string $namespace = 'default')

Unset data from the session store

void
start()

Start a session.

boolean
destroy()

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

boolean
restart()

Restart an expired or locked session.

boolean
fork()

Create a new session and copy variables from the old one

void
close()

Writes session data and ends session

boolean
gc()

Delete expired session data

void
setHandler( JSessionHandlerInterface $handler)

Set the session handler

Details

__construct( string $store = 'none', array $options = array(), JSessionHandlerInterface $handlerInterface = null)

Constructor

Parameters

string $store The type of storage for the session.
array $options Optional parameters
JSessionHandlerInterface $handlerInterface The session handler

mixed __get( string $name)

Magic method to get read-only access to properties.

Parameters

string $name Name of property to retrieve

Return Value

mixed The value of the property

static Session getInstance( string $store, array $options, JSessionHandlerInterface $handlerInterface = null)

Returns the global Session object, only creating it if it doesn't already exist.

Parameters

string $store The type of storage for the session.
array $options An array of configuration options.
JSessionHandlerInterface $handlerInterface The session handler

Return Value

Session The Session object.

string getState()

Get current state of session

Return Value

string The session state

integer getExpire()

Get expiration time in seconds

Return Value

integer The session expiration time in seconds

string getToken( boolean $forceNew = false)

Get a session token, if a token isn't set yet one will be generated.

Tokens are used to secure forms from spamming attacks. Once a token has been generated the system will check the post request to see if it is present, if not it will invalidate the session.

Parameters

boolean $forceNew If true, force a new token to be created

Return Value

string The session token

boolean hasToken( string $tCheck, boolean $forceExpire = true)

Method to determine if a token exists in the session. If not the session will be set to expired

Parameters

string $tCheck Hashed token to be verified
boolean $forceExpire If true, expires the session

Return Value

boolean

static string getFormToken( boolean $forceNew = false)

Method to determine a hash for anti-spoofing variable names

Parameters

boolean $forceNew If true, force a new token to be created

Return Value

string Hashed var name

ArrayIterator getIterator()

Retrieve an external iterator.

Return Value

ArrayIterator

static boolean checkToken( string $method = 'post')

Checks for a form token in the request.

Use in conjunction with \JHtml::_('form.token') or Session::getFormToken.

Parameters

string $method The request method in which to look for the token key.

Return Value

boolean True if found and valid, false otherwise.

string getName()

Get session name

Return Value

string The session name

string getId()

Get session id

Return Value

string The session id

Registry getData()

Returns a clone of the internal data pointer

Return Value

Registry

static array getStores()

Get the session handlers

Return Value

array An array of available session handlers

boolean isActive()

Shorthand to check if the session is active

Return Value

boolean

boolean isNew()

Check whether this session is currently created

Return Value

boolean True on success.

void initialise( Input $input, JEventDispatcher $dispatcher = null)

Check whether this session is currently created

Parameters

Input $input Input object for the session to use.
JEventDispatcher $dispatcher Dispatcher object for the session to use.

Return Value

void

mixed get( string $name, mixed $default = null, string $namespace = 'default')

Get data from the session store

Parameters

string $name Name of a variable
mixed $default Default value of a variable if not set
string $namespace Namespace to use, default to 'default'

Return Value

mixed Value of a variable

mixed set( string $name, mixed $value = null, string $namespace = 'default')

Set data into the session store.

Parameters

string $name Name of a variable.
mixed $value Value of a variable.
string $namespace Namespace to use, default to 'default'.

Return Value

mixed Old value of a variable.

boolean has( string $name, string $namespace = 'default')

Check whether data exists in the session store

Parameters

string $name Name of variable
string $namespace Namespace to use, default to 'default'

Return Value

boolean True if the variable exists

mixed clear( string $name, string $namespace = 'default')

Unset data from the session store

Parameters

string $name Name of variable
string $namespace Namespace to use, default to 'default'

Return Value

mixed The value from session or NULL if not set

void start()

Start a session.

Return Value

void

boolean destroy()

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

This method resets the data pointer and destroys all of the data associated with the current session in its storage. It forces a new session to be started after this method is called. It does not unset the session cookie.

Return Value

boolean True on success

See also

session_destroy()
session_unset()

boolean restart()

Restart an expired or locked session.

Return Value

boolean True on success

See also

Session::destroy()

boolean fork()

Create a new session and copy variables from the old one

Return Value

boolean $result true on success

void close()

Writes session data and ends session

Session data is usually stored after your script terminated without the need to call Session::close(), but as session data is locked to prevent concurrent writes only one script may operate on a session at any time. When using framesets together with sessions you will experience the frames loading one by one due to this locking. You can reduce the time needed to load all the frames by ending the session as soon as all changes to session variables are done.

Return Value

void

boolean gc()

Delete expired session data

Return Value

boolean True on success, false otherwise.

void setHandler( JSessionHandlerInterface $handler)

Set the session handler

Parameters

JSessionHandlerInterface $handler The session handler

Return Value

void