Session
class Session
Handles all manipulation of the session.
An instance of a Session
object can be retrieved via an HTTPRequest
by calling the getSession()
method.
In order to support things like testing, the session is associated with a particular Controller. In normal usage, this is loaded from and saved to the regular PHP session, but for things like static-page-generation and unit-testing, you can create multiple Controllers, each with their own session.
Saving Data
Once you've retrieved a session instance, you can write a value to a users session using the function {@link Session::set()}.
$request->getSession()->set('MyValue', 6);
Saves the value of "6" to the MyValue session data. You can also save arrays or serialized objects in session (but note there may be size restrictions as to how much you can save)
$session = $request->getSession();
// save a variable
$var = 1;
$session->set('MyVar', $var);
// saves an array
$session->set('MyArrayOfValues', array('1', '2', '3'));
// saves an object (you'll have to unserialize it back)
$object = new Object();
$session->set('MyObject', serialize($object));
Accessing Data
Once you have saved a value to the Session you can access it by using the {@link Session::get()} function. Note that session data isn't persisted in PHP's own session store (via $_SESSION) until {@link Session::save()} is called, which happens automatically at the end of a standard request through {@link SilverStripe\Control\Middleware\SessionMiddleware}.
The values in the comments are the values stored from the previous example.
public function bar() {
$session = $this->getRequest()->getSession();
$value = $session->get('MyValue'); // $value = 6
$var = $session->get('MyVar'); // $var = 1
$array = $session->get('MyArrayOfValues'); // $array = array(1,2,3)
$object = $session->get('MyObject', unserialize($object)); // $object = Object()
}
You can also get all the values in the session at once. This is useful for debugging.
$session->getAll(); // returns an array of all the session values.
Clearing Data
Once you have accessed a value from the Session it doesn't automatically wipe the value from the Session, you have to specifically remove it. To clear a value you can either delete 1 session value by the name that you saved it
$session->clear('MyValue'); // MyValue is no longer 6.
Or you can clear every single value in the session at once. Note SilverStripe stores some of its own session data
including form and page comment information. None of this is vital but clearAll()
will clear everything.
$session->clearAll();
Traits
Config options
timeout | int | Set session timeout in seconds. | |
session_ips | array | ||
cookie_domain | string | ||
cookie_path | string | ||
session_store_path | string | ||
cookie_secure | boolean | ||
cookie_name_secure | string | ||
strict_user_agent_check | bool | Invalidate the session if user agent header changes between request. Defaults to true. Disabling this checks is not recommended. |
Methods
Get a configuration accessor for this class. Short hand for Config::inst()->get($this->class, .
Gets the uninherited value for the given config option
Start PHP session, then create a new Session object with the given start data.
Init this session instance before usage, if a session identifier is part of the passed in request.
Determine if this session has started
Begin session, regardless if a session identifier is present in the request, or whether any session data needs to be written.
Destroy this session
Set session value
Merge value with array
Get session value
Clear session value
Clear all values
Get all values
Save data to session Only save the changes, so that anyone manipulating $_SESSION directly doesn't get burned.
Returns the list of changed keys
Details
in Configurable at line 20
static Config_ForClass
config()
Get a configuration accessor for this class. Short hand for Config::inst()->get($this->class, .
....).
in Configurable at line 32
mixed
stat(string $name)
deprecated
deprecated 5.0 Use ->config()->get() instead
Get inherited config value
in Configurable at line 44
mixed
uninherited(string $name)
Gets the uninherited value for the given config option
in Configurable at line 57
$this
set_stat(string $name, mixed $value)
deprecated
deprecated 5.0 Use ->config()->set() instead
Update the config value for a given property
at line 209
__construct(array|null|Session $data)
Start PHP session, then create a new Session object with the given start data.
at line 227
init(HTTPRequest $request)
Init this session instance before usage, if a session identifier is part of the passed in request.
Otherwise, a session might be started in {@link save()} if session data needs to be written with a new session identifier.
at line 250
restart(HTTPRequest $request)
Destroy existing session and restart
at line 261
bool
isStarted()
Determine if this session has started
at line 270
bool
requestContainsSessionId(HTTPRequest $request)
at line 284
start(HTTPRequest $request)
Begin session, regardless if a session identifier is present in the request, or whether any session data needs to be written.
See {@link init()} if you want to "lazy start" a session.
at line 367
destroy(bool $removeCookie = true)
Destroy this session
at line 391
$this
set(string $name, mixed $val)
Set session value
at line 433
addToArray(string $name, mixed $val)
Merge value with array
at line 456
mixed
get(string $name)
Get session value
at line 467
$this
clear(string $name)
Clear session value
at line 491
clearAll()
Clear all values
at line 505
array|null
getAll()
Get all values
at line 515
finalize(HTTPRequest $request)
Set user agent key
at line 526
save(HTTPRequest $request)
Save data to session Only save the changes, so that anyone manipulating $_SESSION directly doesn't get burned.
at line 568
array
changedData()
Returns the list of changed keys