CHttpCookie.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * CHttpCookie class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * A CHttpCookie instance stores a single cookie, including the cookie name, value, domain, path, expire, and secure.
  12. *
  13. * @author Qiang Xue <qiang.xue@gmail.com>
  14. * @package system.web
  15. * @since 1.0
  16. */
  17. class CHttpCookie extends CComponent
  18. {
  19. /**
  20. * @var string name of the cookie
  21. */
  22. public $name;
  23. /**
  24. * @var string value of the cookie
  25. */
  26. public $value='';
  27. /**
  28. * @var string domain of the cookie
  29. */
  30. public $domain='';
  31. /**
  32. * @var integer the timestamp at which the cookie expires. This is the server timestamp. Defaults to 0, meaning "until the browser is closed".
  33. */
  34. public $expire=0;
  35. /**
  36. * @var string the path on the server in which the cookie will be available on. The default is '/'.
  37. */
  38. public $path='/';
  39. /**
  40. * @var boolean whether cookie should be sent via secure connection
  41. */
  42. public $secure=false;
  43. /**
  44. * @var boolean whether the cookie should be accessible only through the HTTP protocol.
  45. * By setting this property to true, the cookie will not be accessible by scripting languages,
  46. * such as JavaScript, which can effectly help to reduce identity theft through XSS attacks.
  47. * Note, this property is only effective for PHP 5.2.0 or above.
  48. */
  49. public $httpOnly=false;
  50. /**
  51. * Constructor.
  52. * @param string $name name of this cookie
  53. * @param string $value value of this cookie
  54. * @param array $options the configuration array consisting of name-value pairs
  55. * that are used to configure this cookie
  56. */
  57. public function __construct($name,$value,$options=array())
  58. {
  59. $this->name=$name;
  60. $this->value=$value;
  61. $this->configure($options);
  62. }
  63. /**
  64. * This method can be used to configure the CookieObject with an array
  65. * Note: you cannot use this method to set the name and/or the value of the cookie
  66. * @param array $options the configuration array consisting of name-value pairs
  67. * that are used to configure this cookie
  68. * @since 1.1.11
  69. */
  70. public function configure($options=array())
  71. {
  72. foreach($options as $name=>$value)
  73. {
  74. if($name==='name'||$name==='value')
  75. continue;
  76. $this->$name=$value;
  77. }
  78. }
  79. /**
  80. * Magic method to use the cookie object as a string without having to call value property first.
  81. * <code>
  82. * $value = (string)$cookies['name'];
  83. * </code>
  84. * Note, that you still have to check if the cookie exists.
  85. * @return string The value of the cookie. If the value property is null an empty string will be returned.
  86. * @since 1.1.11
  87. */
  88. public function __toString()
  89. {
  90. return (string)$this->value;
  91. }
  92. }