Cookie.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\web;
  8. /**
  9. * Cookie represents information related with a cookie, such as [[name]], [[value]], [[domain]], etc.
  10. *
  11. * For more details and usage information on Cookie, see the [guide article on handling cookies](guide:runtime-sessions-cookies).
  12. *
  13. * @author Qiang Xue <qiang.xue@gmail.com>
  14. * @since 2.0
  15. */
  16. class Cookie extends \yii\base\BaseObject
  17. {
  18. /**
  19. * SameSite policy Lax will prevent the cookie from being sent by the browser in all cross-site browsing context
  20. * during CSRF-prone request methods (e.g. POST, PUT, PATCH etc).
  21. * E.g. a POST request from https://otherdomain.com to https://yourdomain.com will not include the cookie, however a GET request will.
  22. * When a user follows a link from https://otherdomain.com to https://yourdomain.com it will include the cookie
  23. * @see $sameSite
  24. */
  25. const SAME_SITE_LAX = 'Lax';
  26. /**
  27. * SameSite policy Strict will prevent the cookie from being sent by the browser in all cross-site browsing context
  28. * regardless of the request method and even when following a regular link.
  29. * E.g. a GET request from https://otherdomain.com to https://yourdomain.com or a user following a link from
  30. * https://otherdomain.com to https://yourdomain.com will not include the cookie.
  31. * @see $sameSite
  32. */
  33. const SAME_SITE_STRICT = 'Strict';
  34. /**
  35. * @var string name of the cookie
  36. */
  37. public $name;
  38. /**
  39. * @var string value of the cookie
  40. */
  41. public $value = '';
  42. /**
  43. * @var string domain of the cookie
  44. */
  45. public $domain = '';
  46. /**
  47. * @var int the timestamp at which the cookie expires. This is the server timestamp.
  48. * Defaults to 0, meaning "until the browser is closed".
  49. */
  50. public $expire = 0;
  51. /**
  52. * @var string the path on the server in which the cookie will be available on. The default is '/'.
  53. */
  54. public $path = '/';
  55. /**
  56. * @var bool whether cookie should be sent via secure connection
  57. */
  58. public $secure = false;
  59. /**
  60. * @var bool whether the cookie should be accessible only through the HTTP protocol.
  61. * By setting this property to true, the cookie will not be accessible by scripting languages,
  62. * such as JavaScript, which can effectively help to reduce identity theft through XSS attacks.
  63. */
  64. public $httpOnly = true;
  65. /**
  66. * @var string SameSite prevents the browser from sending this cookie along with cross-site requests.
  67. * Please note that this feature is only supported since PHP 7.3.0
  68. * For better security, an exception will be thrown if `sameSite` is set while using an unsupported version of PHP.
  69. * To use this feature across different PHP versions check the version first. E.g.
  70. * ```php
  71. * $cookie->sameSite = PHP_VERSION_ID >= 70300 ? yii\web\Cookie::SAME_SITE_LAX : null,
  72. * ```
  73. * See https://www.owasp.org/index.php/SameSite for more information about sameSite.
  74. *
  75. * @since 2.0.21
  76. */
  77. public $sameSite;
  78. /**
  79. * Magic method to turn a cookie object into a string without having to explicitly access [[value]].
  80. *
  81. * ```php
  82. * if (isset($request->cookies['name'])) {
  83. * $value = (string) $request->cookies['name'];
  84. * }
  85. * ```
  86. *
  87. * @return string The value of the cookie. If the value property is null, an empty string will be returned.
  88. */
  89. public function __toString()
  90. {
  91. return (string) $this->value;
  92. }
  93. }