class Session implements ArrayAccess, IteratorAggregate (View source)

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.

Constants

STATE_ACTIVE

STATE_INACTIVE

STATE_EXPIRED

STATE_DESTROYED

STATE_ERROR

Methods

__construct(HandlerInterface $handler = null, SessionBagInterface $bag = null, FlashBagInterface $flashBag = null, SessionBridgeInterface $bridge = null, array $options = array())

Session constructor.

boolean
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

close()

Writes session data and ends session

regenerate(bool $destroy = false)

Re generate the session id.

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

Get data from the session store

array
getAll(string $namespace = 'default')

Get all session data.

array
takeAll(string $namespace = 'default')

Get all session data and clean them.

clean(string $namespace = 'default')

Clean all data from a bag (namespace).

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

Set data into the session store.

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

Check whether data exists in the session store

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

Unset data from the session store

addFlash(array|string $msg, string $type = 'info')

Add a flash message.

array
getFlashes()

Take all flashes and clean them from bag.

getIterator(string $namespace = 'default')

Retrieve an external iterator

getBridge()

Method to get property Bridge

setBridge(SessionBridgeInterface $bridge)

Method to set property bridge

getHandler()

Method to get property Handler

setHandler(HandlerInterface $handler)

Method to set property handler

string
getName()

Get session name

string
getId()

Get session id

boolean
isActive()

Shorthand to check if the session is active

boolean
isNew()

Check whether this session is currently created

string
getState()

Method to get property State

setState(string $state)

Method to set property state

array
getCookie()

Get cookie source, default is $_COOKIE.

setCookie(array $cookie)

Set cookie source. default will be $_COOKIE.

mixed
getOption(string $name, mixed $default = null)

Method to get property Options

setOption(string $name, mixed $value)

Method to set property options

array
getOptions()

Method to get property Options

setOptions(array $options)

Method to set property options

array
getBags()

Get all Session bags.

setBags(array $bags)

Set Session bags.

getBag(string $name)

Get session bag.

setBag(string $name, SessionBagInterface $bag)

Set session bag by name.

getFlashBag()

Get Flash bag.

setFlashBag(FlashBagInterface $bag)

Set Flash Bag

setDebug(boolean $debug)

Method to set property debug

boolean
offsetExists(mixed $offset)

Is a property exists or not.

mixed
offsetGet(mixed $offset)

Get a property.

void
offsetSet(mixed $offset, mixed $value)

Set a value to property.

void
offsetUnset(mixed $offset)

Unset a property.

Details

at line line 114
__construct(HandlerInterface $handler = null, SessionBagInterface $bag = null, FlashBagInterface $flashBag = null, SessionBridgeInterface $bridge = null, array $options = array())

Session constructor.

Parameters

HandlerInterface $handler The type of storage for the session.
SessionBagInterface $bag The session data bags.
FlashBagInterface $flashBag The session flash bags.
SessionBridgeInterface $bridge The session bridge, default is PHP NativeBridge. Set this to your own bridge to help test.
array $options Optional parameters.

at line line 207
boolean start()

Start a session.

Return Value

boolean

at line line 262
boolean destroy()

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

This method resets the $_SESSION variable and destroys all of the data associated with the current session in its storage (file or DB). It forces 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()

at line line 296
boolean restart()

Restart an expired or locked session.

Return Value

boolean True on success

Exceptions

RuntimeException

See also

destroy

at line line 323
boolean fork()

Create a new session and copy variables from the old one

Return Value

boolean $result true on success

Exceptions

RuntimeException

at line line 364
Session close()

Writes session data and ends session

Session data is usually stored after your script terminated without the need to call 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

Session

See also

session_write_close()

at line line 382
Session regenerate(bool $destroy = false)

Re generate the session id.

Parameters

bool $destroy Destroy session or not.

Return Value

Session

at line line 513
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

Exceptions

RuntimeException

at line line 538
array getAll(string $namespace = 'default')

Get all session data.

Parameters

string $namespace Session namespace, default is default.

Return Value

array

Exceptions

RuntimeException

at line line 563
array takeAll(string $namespace = 'default')

Get all session data and clean them.

Parameters

string $namespace Session namespace, default is default.

Return Value

array

Exceptions

RuntimeException

at line line 592
Session clean(string $namespace = 'default')

Clean all data from a bag (namespace).

Parameters

string $namespace $namespace Session namespace, default is default.

Return Value

Session Return self to support chaining.

at line line 611
Session 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

Session Return self to support chaining.

Exceptions

RuntimeException

at line line 639
boolean exists(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

Exceptions

RuntimeException

at line line 665
mixed remove(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

Exceptions

RuntimeException

at line line 692
Session addFlash(array|string $msg, string $type = 'info')

Add a flash message.

Parameters

array|string $msg The message you want to set, can be an array to storage multiple messages.
string $type The message type, default is info.

Return Value

Session

at line line 706
array getFlashes()

Take all flashes and clean them from bag.

Return Value

array All flashes data.

at line line 720
Traversable getIterator(string $namespace = 'default')

Retrieve an external iterator

Parameters

string $namespace The namespace to get data.

Return Value

Traversable An instance of an object implementing Iterator Traversable

at line line 734
SessionBridgeInterface getBridge()

Method to get property Bridge

Return Value

SessionBridgeInterface

at line line 748
Session setBridge(SessionBridgeInterface $bridge)

Method to set property bridge

Parameters

SessionBridgeInterface $bridge

Return Value

Session Return self to support chaining.

at line line 762
HandlerInterface getHandler()

Method to get property Handler

Return Value

HandlerInterface

at line line 776
Session setHandler(HandlerInterface $handler)

Method to set property handler

Parameters

HandlerInterface $handler

Return Value

Session Return self to support chaining.

at line line 790
string getName()

Get session name

Return Value

string The session name

at line line 802
string getId()

Get session id

Return Value

string The session name

at line line 814
boolean isActive()

Shorthand to check if the session is active

Return Value

boolean

at line line 826
boolean isNew()

Check whether this session is currently created

Return Value

boolean True on success.

at line line 864
string getState()

Method to get property State

Return Value

string

at line line 878
Session setState(string $state)

Method to set property state

Parameters

string $state

Return Value

Session Return self to support chaining.

at line line 892
array getCookie()

Get cookie source, default is $_COOKIE.

Return Value

array Cookie source

at line line 913
Session setCookie(array $cookie)

Set cookie source. default will be $_COOKIE.

Set this property to your array to help you test.

Parameters

array $cookie Cookie source.

Return Value

Session Return self to support chaining.

at line line 930
mixed getOption(string $name, mixed $default = null)

Method to get property Options

Parameters

string $name
mixed $default

Return Value

mixed

at line line 950
Session setOption(string $name, mixed $value)

Method to set property options

Parameters

string $name
mixed $value

Return Value

Session Return self to support chaining.

at line line 964
array getOptions()

Method to get property Options

Return Value

array

at line line 978
Session setOptions(array $options)

Method to set property options

Parameters

array $options

Return Value

Session Return self to support chaining.

at line line 1006
array getBags()

Get all Session bags.

Return Value

array

at line line 1020
Session setBags(array $bags)

Set Session bags.

Parameters

array $bags

Return Value

Session Return self to support chaining.

at line line 1040
SessionBagInterface getBag(string $name)

Get session bag.

Parameters

string $name Bag name to get.

Return Value

SessionBagInterface

Exceptions

UnexpectedValueException

at line line 1062
Session setBag(string $name, SessionBagInterface $bag)

Set session bag by name.

Parameters

string $name Session bag name to set.
SessionBagInterface $bag Session bag object.

Return Value

Session

at line line 1081
FlashBagInterface getFlashBag()

Get Flash bag.

Return Value

FlashBagInterface

at line line 1100
Session setFlashBag(FlashBagInterface $bag)

Set Flash Bag

Parameters

FlashBagInterface $bag The flash bag object.

Return Value

Session

at line line 1144
Session setDebug(boolean $debug)

Method to set property debug

Parameters

boolean $debug

Return Value

Session Return self to support chaining.

at line line 1160
boolean offsetExists(mixed $offset)

Is a property exists or not.

Parameters

mixed $offset Offset key.

Return Value

boolean

at line line 1175
mixed offsetGet(mixed $offset)

Get a property.

Parameters

mixed $offset Offset key.

Return Value

mixed The value to return.

Exceptions

InvalidArgumentException

at line line 1191
void offsetSet(mixed $offset, mixed $value)

Set a value to property.

Parameters

mixed $offset Offset key.
mixed $value The value to set.

Return Value

void

Exceptions

InvalidArgumentException

at line line 1206
void offsetUnset(mixed $offset)

Unset a property.

Parameters

mixed $offset Offset key to unset.

Return Value

void

Exceptions

InvalidArgumentException