Session
class Session (View source)
Handles all manipulation of the session.
The static methods are used to manipulate the currently active controller's session. The instance methods are used to manipulate a particular session. There can be more than one of these created.
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.
The instance object is basically just a way of manipulating a set of nested maps, and isn't specific to session data.
Saving Data
You can write a value to a users session from your PHP code using the static function Session::set(). You can add this line in any function or file you wish to save the value.
Session::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)
// 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 Session::get() function. Like the Session::set() function you can use this anywhere in your PHP files.
The values in the comments are the values stored from the previous example.
public function bar() {
$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::get_all(); // 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 clear_all will clear everything.
Session::clear_all();
Properties
protected | $data | Session data |
||
protected | $changedData | |||
protected static | $default_session |
Methods
Start PHP session, then create a new Session object with the given start data.
Path to set on the domain where the session cookie will work.
Secure cookie, tells the browser to only send it over SSL.
Provide an array
of rules specifing timeouts for IPv4 address ranges or
individual IPv4 addresses. The key is an IP address or range and the value is the time
until the session expires in seconds. For example:
Save data to session Only save the changes, so that anyone manipulating $_SESSION directly doesn't get burned.
Recursively apply the changes represented in $data to $dest.
Sets the appropriate form message in session, with type. This will be shown once, for the form specified.
Details
protected
userAgent()
No description
__construct($data)
Start PHP session, then create a new Session object with the given start data.
static
set_cookie_domain(string $domain)
deprecated
deprecated
Cookie domain, for example 'www.php.net'.
To make cookies visible on all subdomains then the domain must be prefixed with a dot like '.php.net'.
static string
get_cookie_domain()
deprecated
deprecated
Get the cookie domain.
static
set_cookie_path(string $path)
deprecated
deprecated
Path to set on the domain where the session cookie will work.
Use a single slash ('/') for all paths on the domain.
static string
get_cookie_path()
deprecated
deprecated
Get the path on the domain where the session cookie will work.
static
set_cookie_secure(bool $secure)
deprecated
deprecated
Secure cookie, tells the browser to only send it over SSL.
static bool
get_cookie_secure()
deprecated
deprecated
Get if the cookie is secure
static
set_session_store_path(string $path)
deprecated
deprecated
Set the session store path
static string
get_session_store_path()
deprecated
deprecated
Get the session store path
static
set_timeout_ips($ips)
deprecated
deprecated
Provide an array
of rules specifing timeouts for IPv4 address ranges or
individual IPv4 addresses. The key is an IP address or range and the value is the time
until the session expires in seconds. For example:
Session::set_timeout_ips(array( '127.0.0.1' => 36000 ));
Any user connecting from 127.0.0.1 (localhost) will have their session expired after 10 hours.
Session::set_timeout is used to set the timeout value for any users whose address is not in the given IP range.
static
add_to_array($name, $val)
Add a value to a specific key in the session array
static
set(string $name, string $val)
Set a key/value pair in the session
static
get(string $name)
Return a specific value by session key
static array
get_all()
Return all the values in session
static
clear(string $name)
Clear a given session key, value pair.
static void
clear_all()
Clear all the values
static
save()
Save all the values in our session to $_SESSION
static protected
current_session()
No description
inst_start($sid = null)
No description
inst_destroy($removeCookie = true)
No description
inst_set($name, $val)
No description
inst_addToArray($name, $val)
No description
inst_get($name)
No description
inst_clear($name)
No description
inst_clearAll()
No description
inst_getAll()
No description
inst_finalize()
No description
inst_save()
Save data to session Only save the changes, so that anyone manipulating $_SESSION directly doesn't get burned.
protected
recursivelyApply($data, $dest)
Recursively apply the changes represented in $data to $dest.
Used to update $_SESSION
array
inst_changedData()
Return the changed data, for debugging purposes.
static
setFormMessage(string $formname, string $message, string $type)
Sets the appropriate form message in session, with type. This will be shown once, for the form specified.
static bool
request_contains_session_id()
Is there a session ID in the request?
static
start(string $sid = null)
Initialize session.
static
destroy(bool $removeCookie = true)
Destroy the active session.
static
set_timeout(int $timeout)
deprecated
deprecated
Set the timeout of a Session value
static
get_timeout()
deprecated
deprecated
No description