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