Class Session
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.
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::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 clear_all will clear everything.
Session::clearAll();
Methods summary
public static
|
|
public static
string
|
|
public static
|
#
set_cookie_path( string $path )
Path to set on the domain where the session cookie will work. Use a single slash ('/') for all paths on the domain. |
public static
string
|
|
public static
|
|
public static
boolean
|
|
public
|
|
public static
|
#
set_timeout_ips( array $session_ips )
Provide an |
public static
|
|
public static
|
|
public static
|
|
public static
|
|
public static
Array
|
|
public static
|
|
public static
|
|
public static
|
|
public static
|
|
public static
|
|
protected static
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
protected
|
#
recursivelyApply( mixed $data, mixed & $dest )
Recursively apply the changes represented in $data to $dest. Used to update $_SESSION |
public static
|
#
setFormMessage( formname $formname, messsage $message, type $type )
Sets the appropriate form message in session, with type. This will be shown once, for the form specified. |
public static
|
|
public static
|
|
public static
|
#
load_config( )
Use the Session::$session_ips array to set timeouts based on IP address or IP address range. |
public static
|
|
public static
|
Magic methods summary
Properties summary
protected static
integer
|
$timeout |
#
Set session timeout |
protected static
array
|
$session_ips | |
protected static
mixed
|
$cookie_domain | |
protected static
mixed
|
$cookie_path | |
protected static
boolean
|
$cookie_secure | |
protected
array
|
$data |
#
Session data |
protected
array
|
$changedData | |
protected static
mixed
|
$default_session |