From ff91f99522c5d3a57ffb4175925415302bf72b0f Mon Sep 17 00:00:00 2001 From: Will Rossiter Date: Tue, 28 Oct 2014 19:03:27 +1300 Subject: [PATCH] Add brief highlevel docs on cookie / session --- .../18_Cookies_And_Sessions/01_Cookies.md | 40 +++++++++++ .../18_Cookies_And_Sessions/02_Sessions.md | 68 +++++++++++++++++++ .../18_Cookies_And_Sessions/index.md | 10 +++ 3 files changed, 118 insertions(+) create mode 100644 docs/en/02_Developer_Guides/18_Cookies_And_Sessions/01_Cookies.md create mode 100644 docs/en/02_Developer_Guides/18_Cookies_And_Sessions/02_Sessions.md create mode 100644 docs/en/02_Developer_Guides/18_Cookies_And_Sessions/index.md diff --git a/docs/en/02_Developer_Guides/18_Cookies_And_Sessions/01_Cookies.md b/docs/en/02_Developer_Guides/18_Cookies_And_Sessions/01_Cookies.md new file mode 100644 index 000000000..32ae7cea1 --- /dev/null +++ b/docs/en/02_Developer_Guides/18_Cookies_And_Sessions/01_Cookies.md @@ -0,0 +1,40 @@ +title: Cookies +summary: A set of static methods for manipulating PHP cookies. + +# Cookies + +Cookies are a mechanism for storing data in the remote browser and thus tracking or identifying return users. +SilverStripe uses cookies for remembering users preferences. Application code can modify a users cookies through +the [api:Cookie] class. This class mostly follows the PHP API. + +## set + +Sets the value of cookie with configuration. + + :::php + Cookie::set($name, $value, $expiry = 90, $path = null, $domain = null, $secure = false, $httpOnly = false); + + // Cookie::set('MyApplicationPreference', 'Yes'); + +## get + +Returns the value of cookie. + + :::php + Cookie::get($name); + + // Cookie::get('MyApplicationPreference'); + // returns 'Yes' + +## force_expiry + +Clears a given cookie. + + :::php + Cookie::force_expiry($name, $path = null, $domain = null); + + // Cookie::force_expiry('MyApplicationPreference') + +## API Documentation + +* [api:Cookie] \ No newline at end of file diff --git a/docs/en/02_Developer_Guides/18_Cookies_And_Sessions/02_Sessions.md b/docs/en/02_Developer_Guides/18_Cookies_And_Sessions/02_Sessions.md new file mode 100644 index 000000000..7a48fc53b --- /dev/null +++ b/docs/en/02_Developer_Guides/18_Cookies_And_Sessions/02_Sessions.md @@ -0,0 +1,68 @@ +title: Sessions +summary: A set of static methods for manipulating PHP sessions. + +# Sessions + +Session support in PHP consists of a way to preserve certain data across subsequent accesses such as logged in user +information and security tokens. + +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. + +## set + + :::php + Session::set('MyValue', 6); + +Saves the value of to 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). + + :::php + // 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)); + +## get + +Once you have saved a value to the Session you can access it by using the `get` function. Like the `set` function you +can use this anywhere in your PHP files. + + :::php + echo Session::get('MyValue'); + // returns 6 + + $data = Session::get('MyArrayOfValues'); + // $data = array(1,2,3) + + $object = unserialize(Session::get('MyObject', $object)); + // $object = Object() + +## get_all + +You can also get all the values in the session at once. This is useful for debugging. + + :::php + Session::get_all(); + // returns an array of all the session values. + +## clear + +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. + + :::php + Session::clear('MyValue'); + +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. + + :::php + Session::clear_all(); + +## API Documentation + +* [api:Session] \ No newline at end of file diff --git a/docs/en/02_Developer_Guides/18_Cookies_And_Sessions/index.md b/docs/en/02_Developer_Guides/18_Cookies_And_Sessions/index.md new file mode 100644 index 000000000..76af506d0 --- /dev/null +++ b/docs/en/02_Developer_Guides/18_Cookies_And_Sessions/index.md @@ -0,0 +1,10 @@ +title: Cookies and Sessions +summary: Save state information using the Cookie class and the Session class. +introduction: Both the Cookie and Session classes can be used to preserve certain data across subsequent page requests. + +[CHILDREN] + +## API Documentation + +* [api:Cookie] +* [api:Session] \ No newline at end of file