Application::getSession
Get SESSION data
Description
public mixed Application::getSession( string $name = '' )
Get SESSION data. If the key does not exist, returns ''. If you omit the argument, returns all SESSION data as an array.
getSession() does not create a session: when the request has no session cookie (and nothing was written to the session yet), it returns '' (or array() without an argument) and no cookie is sent. An existing session is opened to read it.
Arguments
| Name | Data type | Default | Remarks |
|---|---|---|---|
| $name | string | '' | Session key. All SESSION data when omitted |
Example
Controller - First request
$this->setSession('foo', 'bar');
Controller - Second request
echo $this->getSession('foo'); // -> bar
echo $this->getSession('none'); // -> '' (empty string)
Get all session data
If you omit the argument, getSession returns all SESSION data.
Controller - First request
$this->setSession('foo', 'bar');
$this->setSession('baz', 'qux');
Controller - Second request
var_dump( $this->getSession() );
Output
array(2) { ["foo"]=> string(3) "bar" ["baz"]=> string(3) "qux" }