PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/hiromi2424/docs
ReStructuredText | 183 lines | 138 code | 45 blank | 0 comment | 0 complexity | 9868296420cab984e12f4fcac0b50457 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 controller's $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. | | | The string should be random and difficult to guess. |
  23. | | | |
  24. | | | When using rijndael or aes encryption, this value |
  25. | | | must be longer than 32 bytes. |
  26. +-----------------+--------------+------------------------------------------------------+
  27. | string $domain | '' | The domain name allowed to access the cookie. For |
  28. | | | example, use '.yourdomain.com' to allow access from |
  29. | | | all your subdomains. |
  30. +-----------------+--------------+------------------------------------------------------+
  31. | int or string | '5 Days' | The time when your cookie will expire. Integers are |
  32. | $time | | interpreted as seconds. 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 $path is set to '/foo/', the cookie will |
  40. | | | only be available within the /foo/ directory and all |
  41. | | | sub-directories of your domain, such as /foo/bar. 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. public $components = array('Cookie');
  60. public function beforeFilter() {
  61. parent::beforeFilter();
  62. $this->Cookie->name = 'baker_id';
  63. $this->Cookie->time = 3600; // or '1 hour'
  64. $this->Cookie->path = '/bakers/preferences/';
  65. $this->Cookie->domain = 'example.com';
  66. $this->Cookie->secure = true; // i.e. only sent if using secure HTTPS
  67. $this->Cookie->key = 'qSI232qs*&sXOw!adre@34SAv!@*(XSL#$%)asGb$@11~_+!@#HKis~#^';
  68. $this->Cookie->httpOnly = true;
  69. $this->Cookie->type('aes');
  70. }
  71. Next, let's 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 the cookie component. $key is the
  78. cookie variable name you want, and the $value is the information to
  79. be stored::
  80. $this->Cookie->write('name', 'Larry');
  81. You can also group your variables by using dot notation in the
  82. key parameter::
  83. $this->Cookie->write('User.name', 'Larry');
  84. $this->Cookie->write('User.role', 'Lead');
  85. If you want to write more than one value to the cookie at a time,
  86. you can pass an array::
  87. $this->Cookie->write('User',
  88. array('name' => 'Larry', 'role' => 'Lead')
  89. );
  90. All values in the cookie are encrypted by default. If you want to
  91. store the values as plain text, set the third parameter of the
  92. write() method to false. You should remember to set
  93. the encryption mode to 'aes' to ensure that values are securely
  94. encrypted::
  95. $this->Cookie->write('name', 'Larry', false);
  96. The last parameter to write is $expires the number of seconds
  97. until your cookie will expire. For convenience, this parameter can
  98. also be passed as a string that the PHP strtotime() function
  99. understands::
  100. // Both cookies expire in one hour.
  101. $this->Cookie->write('first_name', 'Larry', false, 3600);
  102. $this->Cookie->write('last_name', 'Masters', false, '1 hour');
  103. .. php:method:: read(mixed $key = null)
  104. This method is used to read the value of a cookie variable with the
  105. name specified by $key. ::
  106. // Outputs "Larry"
  107. echo $this->Cookie->read('name');
  108. // You can also use the dot notation for read
  109. echo $this->Cookie->read('User.name');
  110. // To get the variables which you had grouped
  111. // using the dot notation as an array use the following
  112. $this->Cookie->read('User');
  113. // this outputs something like array('name' => 'Larry', 'role' => 'Lead')
  114. .. php:method:: check($key)
  115. :param string $key: The key to check.
  116. Used to check whether a key/path exists and has a non-null value.
  117. .. versionadded:: 2.3
  118. ``CookieComponent::check()`` was added in 2.3
  119. .. php:method:: delete(mixed $key)
  120. Deletes a cookie variable of the name in $key. Works with dot
  121. notation::
  122. // Delete a variable
  123. $this->Cookie->delete('bar');
  124. // Delete the cookie variable bar, but not everything under foo
  125. $this->Cookie->delete('foo.bar');
  126. .. php:method:: destroy()
  127. Destroys the current cookie.
  128. .. php:method:: type($type)
  129. Allows you to change the encryption scheme. By default the 'cipher' scheme is used for
  130. backwards compatibility. However, you should always use either the 'rijndael' or
  131. 'aes' schemes.
  132. .. versionchanged:: 2.2
  133. The 'rijndael' type was added.
  134. .. versionadded:: 2.5
  135. The 'aes' type was added.
  136. .. meta::
  137. :title lang=en: Cookie
  138. :keywords lang=en: array controller,php setcookie,cookie string,controller setup,string domain,default description,string name,session cookie,integers,variables,domain name,null