PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/docs/en/02_Developer_Guides/18_Cookies_And_Sessions/01_Cookies.md

http://github.com/silverstripe/sapphire
Markdown | 154 lines | 96 code | 58 blank | 0 comment | 0 complexity | aabb37afaeabd731a6bd9722709484f8 MD5 | raw file
Possible License(s): BSD-3-Clause, MIT, CC-BY-3.0, GPL-2.0, AGPL-1.0, LGPL-2.1
  1. ---
  2. title: Cookies
  3. summary: A set of static methods for manipulating PHP cookies.
  4. icon: cookie-bite
  5. ---
  6. # Cookies
  7. ## Accessing and Manipulating Cookies
  8. Cookies are a mechanism for storing data in the remote browser and thus tracking or identifying return users.
  9. SilverStripe uses cookies for remembering users preferences. Application code can modify a users cookies through
  10. the [Cookie](api:SilverStripe\Control\Cookie) class. This class mostly follows the PHP API.
  11. ### set
  12. Sets the value of cookie with configuration.
  13. ```php
  14. use SilverStripe\Control\Cookie;
  15. Cookie::set($name, $value, $expiry = 90, $path = null, $domain = null, $secure = false, $httpOnly = false);
  16. // Cookie::set('MyApplicationPreference', 'Yes');
  17. ```
  18. ### get
  19. Returns the value of cookie.
  20. ```php
  21. Cookie::get($name);
  22. // Cookie::get('MyApplicationPreference');
  23. // returns 'Yes'
  24. ```
  25. ### force_expiry
  26. Clears a given cookie.
  27. ```php
  28. Cookie::force_expiry($name, $path = null, $domain = null);
  29. // Cookie::force_expiry('MyApplicationPreference')
  30. ```
  31. ## Cookie_Backend
  32. The [Cookie](api:SilverStripe\Control\Cookie) class manipulates and sets cookies using a [Cookie_Backend](api:SilverStripe\Control\Cookie_Backend). The backend is in charge of the logic
  33. that fetches, sets and expires cookies. By default we use a [CookieJar](api:SilverStripe\Control\CookieJar) backend which uses PHP's
  34. [setcookie](http://www.php.net/manual/en/function.setcookie.php) function.
  35. The [CookieJar](api:SilverStripe\Control\CookieJar) keeps track of cookies that have been set by the current process as well as those that were received
  36. from the browser.
  37. ```php
  38. use SilverStripe\Core\Injector\Injector;
  39. use SilverStripe\Control\Cookie;
  40. use SilverStripe\Control\CookieJar;
  41. $myCookies = [
  42. 'cookie1' => 'value1',
  43. ];
  44. $newBackend = new CookieJar($myCookies);
  45. Injector::inst()->registerService($newBackend, 'Cookie_Backend');
  46. Cookie::get('cookie1');
  47. ```
  48. ### Resetting the Cookie_Backend state
  49. Assuming that your application hasn't messed around with the `$_COOKIE` superglobal, you can reset the state of your
  50. `Cookie_Backend` by simply unregistering the `CookieJar` service with `Injector`. Next time you access `Cookie` it'll
  51. create a new service for you using the `$_COOKIE` superglobal.
  52. ```php
  53. Injector::inst()->unregisterNamedObject('Cookie_Backend');
  54. Cookie::get('cookiename'); // will return $_COOKIE['cookiename'] if set
  55. ```
  56. Alternatively, if you know that the superglobal has been changed (or you aren't sure it hasn't) you can attempt to use
  57. the current `CookieJar` service to tell you what it was like when it was registered.
  58. ```php
  59. //store the cookies that were loaded into the `CookieJar`
  60. $recievedCookie = Cookie::get_inst()->getAll(false);
  61. //set a new `CookieJar`
  62. Injector::inst()->registerService(new CookieJar($recievedCookie), 'CookieJar');
  63. ```
  64. ### Using your own Cookie_Backend
  65. If you need to implement your own Cookie_Backend you can use the injector system to force a different class to be used.
  66. ```yml
  67. ---
  68. Name: mycookie
  69. After: '#cookie'
  70. ---
  71. SilverStripe\Core\Injector\Injector:
  72. Cookie_Backend:
  73. class: MyCookieJar
  74. ```
  75. To be a valid backend your class must implement the [Cookie_Backend](api:SilverStripe\Control\Cookie_Backend) interface.
  76. ## Advanced Usage
  77. ### Sent vs Received Cookies
  78. Sometimes it's useful to be able to tell if a cookie was set by the process (thus will be sent to the browser) or if it
  79. came from the browser as part of the request.
  80. Using the `Cookie_Backend` we can do this like such:
  81. ```php
  82. Cookie::set('CookieName', 'CookieVal');
  83. Cookie::get('CookieName'); //gets the cookie as we set it
  84. //will return the cookie as it was when it was sent in the request
  85. Cookie::get('CookieName', false);
  86. ```
  87. ### Accessing all the cookies at once
  88. One can also access all of the cookies in one go using the `Cookie_Backend`
  89. ```php
  90. Cookie::get_inst()->getAll(); //returns all the cookies including ones set during the current process
  91. Cookie::get_inst()->getAll(false); //returns all the cookies in the request
  92. ```
  93. ## API Documentation
  94. * [Cookie](api:SilverStripe\Control\Cookie)
  95. * [CookieJar](api:SilverStripe\Control\CookieJar)
  96. * [Cookie_Backend](api:SilverStripe\Control\Cookie_Backend)