PageRenderTime 56ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/en/core-libraries/components/cookie.rst

https://github.com/MontBlanc-Sucks/docs
ReStructuredText | 180 lines | 141 code | 39 blank | 0 comment | 0 complexity | 12956446acb7b291fca1c942a9eb1f9d MD5 | raw file
  1. Cookie
  2. ######
  3. .. php:class:: CookieComponent(ComponentCollection $collection, array $settings = array())
  4. The CookieComponent is a wrapper around the native PHP ``setcookie``
  5. method. It also includes a host of delicious icing to make coding
  6. cookies in your controllers very convenient. Before attempting to
  7. use the CookieComponent, you must make sure that 'Cookie' is listed
  8. in your controllers' $components array.
  9. Controller Setup
  10. ================
  11. There are a number of controller variables that allow you to
  12. configure the way cookies are created and managed. Defining these
  13. special variables in the beforeFilter() method of your controller
  14. allows you to define how the CookieComponent works.
  15. +-----------------+--------------+------------------------------------------------------+
  16. | Cookie variable | default | description |
  17. +=================+==============+======================================================+
  18. | string $name |'CakeCookie' | The name of the cookie. |
  19. +-----------------+--------------+------------------------------------------------------+
  20. | string $key | null | This string is used to encrypt |
  21. | | | the value written to the cookie. |
  22. | | | This string should be random and difficult to guess. |
  23. | | | |
  24. | | | When using rijndael encryption this value |
  25. | | | must be longer than 32 bytes. |
  26. +-----------------+--------------+------------------------------------------------------+
  27. | string $domain | '' | The domain name allowed to access the cookie. e.g. |
  28. | | | Use '.yourdomain.com' to allow access from all your |
  29. | | | subdomains. |
  30. +-----------------+--------------+------------------------------------------------------+
  31. | int or string | '5 Days' | The time when your cookie will expire. Integers are |
  32. | $time | | Interpreted as seconds and a value of 0 is equivalent|
  33. | | | to a 'session cookie': i.e. the cookie expires when |
  34. | | | the browser is closed. If a string is set, this will |
  35. | | | be interpreted with PHP function strtotime(). You can|
  36. | | | set this directly within the write() method. |
  37. +-----------------+--------------+------------------------------------------------------+
  38. | string $path | '/' | The server path on which the cookie will be applied. |
  39. | | | If $cookiePath is set to '/foo/', the cookie will |
  40. | | | only be available within the /foo/ directory and all |
  41. | | | sub-directories such as /foo/bar/ of your domain. The|
  42. | | | default value is the entire domain. You can set this |
  43. | | | directly within the write() method. |
  44. +-----------------+--------------+------------------------------------------------------+
  45. | boolean $secure | false | Indicates that the cookie should only be transmitted |
  46. | | | over a secure HTTPS connection. When set to true, the|
  47. | | | cookie will only be set if a secure connection |
  48. | | | exists. You can set this directly within the write() |
  49. | | | method. |
  50. +-----------------+--------------+------------------------------------------------------+
  51. | boolean | false | Set to true to make HTTP only cookies. Cookies that |
  52. | $httpOnly | | are HTTP only are not accessible in Javascript. |
  53. +-----------------+--------------+------------------------------------------------------+
  54. The following snippet of controller code shows how to include the
  55. CookieComponent and set up the controller variables needed to write
  56. a cookie named 'baker\_id' for the domain 'example.com' which needs
  57. a secure connection, is available on the path
  58. /bakers/preferences/, expires in one hour and is HTTP only::
  59. <?php
  60. public $components = array('Cookie');
  61. public function beforeFilter() {
  62. parent::beforeFilter();
  63. $this->Cookie->name = 'baker_id';
  64. $this->Cookie->time = 3600; // or '1 hour'
  65. $this->Cookie->path = '/bakers/preferences/';
  66. $this->Cookie->domain = 'example.com';
  67. $this->Cookie->secure = true; // i.e. only sent if using secure HTTPS
  68. $this->Cookie->key = 'qSI232qs*&sXOw!adre@34SAv!@*(XSL#$%)asGb$@11~_+!@#HKis~#^';
  69. $this->Cookie->httpOnly = true;
  70. }
  71. Next, lets look at how to use the different methods of the Cookie
  72. Component.
  73. Using the Component
  74. ===================
  75. The CookieComponent offers a number of methods for working with Cookies.
  76. .. php:method:: write(mixed $key, mixed $value = null, boolean $encrypt = true, mixed $expires = null)
  77. The write() method is the heart of cookie component, $key is the
  78. cookie variable name you want, and the $value is the information to
  79. be stored::
  80. <?php
  81. $this->Cookie->write('name', 'Larry');
  82. You can also group your variables by supplying dot notation in the
  83. key parameter::
  84. <?php
  85. $this->Cookie->write('User.name', 'Larry');
  86. $this->Cookie->write('User.role', 'Lead');
  87. If you want to write more than one value to the cookie at a time,
  88. you can pass an array::
  89. <?php
  90. $this->Cookie->write('User',
  91. array('name' => 'Larry', 'role' => 'Lead')
  92. );
  93. All values in the cookie are encrypted by default. If you want to
  94. store the values as plain-text, set the third parameter of the
  95. write() method to false. The encryption performed on cookie values
  96. is fairly uncomplicated encryption system. It uses
  97. ``Security.salt`` and a predefined Configure class var
  98. ``Security.cipherSeed`` to encrypt values. To make your cookies
  99. more secure you should change ``Security.cipherSeed`` in
  100. app/Config/core.php to ensure a better encryption.::
  101. <?php
  102. $this->Cookie->write('name', 'Larry', false);
  103. The last parameter to write is $expires the number of seconds
  104. before your cookie will expire. For convenience, this parameter can
  105. also be passed as a string that the php strtotime() function
  106. understands::
  107. <?php
  108. // Both cookies expire in one hour.
  109. $this->Cookie->write('first_name', 'Larry', false, 3600);
  110. $this->Cookie->write('last_name', 'Masters', false, '1 hour');
  111. .. php:method:: read(mixed $key = null)
  112. This method is used to read the value of a cookie variable with the
  113. name specified by $key.::
  114. <?php
  115. // Outputs “Larry”
  116. echo $this->Cookie->read('name');
  117. // You can also use the dot notation for read
  118. echo $this->Cookie->read('User.name');
  119. // To get the variables which you had grouped
  120. // using the dot notation as an array use something like
  121. $this->Cookie->read('User');
  122. // this outputs something like array('name' => 'Larry', 'role' => 'Lead')
  123. .. php:method:: delete(mixed $key)
  124. Deletes a cookie variable of the name in $key. Works with dot
  125. notation::
  126. <?php
  127. // Delete a variable
  128. $this->Cookie->delete('bar');
  129. // Delete the cookie variable bar, but not all under foo
  130. $this->Cookie->delete('foo.bar');
  131. .. php:method:: destroy()
  132. Destroys the current cookie.
  133. .. php:method:: type($type)
  134. Allows you to change the encryption scheme. By default the 'cipher' scheme
  135. is used. However, you should use the 'rijndael' scheme for improved
  136. security.
  137. .. versionchanged:: 2.2
  138. The 'rijndael' type was added.
  139. .. meta::
  140. :title lang=en: Cookie
  141. :keywords lang=en: array controller,php setcookie,cookie string,controller setup,string domain,default description,string name,session cookie,integers,variables,domain name,null