类 yii\web\Cookie
| 继承关系 | yii\web\Cookie » yii\base\BaseObject |
|---|---|
| 实现接口 | yii\base\Configurable |
| 可用版本 | 2.0 |
| 源代码 | https://github.com/yiisoft/yii2/blob/master/framework/web/Cookie.php |
Cookie 表示与 Cookie 相关的信息,例如 $name、$value、$domain 等。
有关 Cookie 的更多详细信息和使用方法,请参阅 处理 Cookie 的指南文章。
公共属性
| 属性 | 类型 | 描述 | 定义于 |
|---|---|---|---|
| $domain | string | Cookie 的域名 | yii\web\Cookie |
| $expire | integer|string|DateTimeInterface|null | Cookie 过期的时间戳或日期。 | yii\web\Cookie |
| $httpOnly | boolean | Cookie 是否只能通过 HTTP 协议访问。 | yii\web\Cookie |
| $name | string | Cookie 的名称 | yii\web\Cookie |
| $path | string | 服务器上 Cookie 可用的路径。 | yii\web\Cookie |
| $sameSite | string | SameSite 阻止浏览器在跨站点请求中发送此 Cookie。 | yii\web\Cookie |
| $secure | boolean | Cookie 是否应该通过安全连接发送 | yii\web\Cookie |
| $value | string | Cookie 的值 | yii\web\Cookie |
公共方法
| 方法 | 描述 | 定义于 |
|---|---|---|
| __call() | 调用不是类方法的命名方法。 | yii\base\BaseObject |
| __construct() | 构造函数。 | yii\base\BaseObject |
| __get() | 返回对象属性的值。 | yii\base\BaseObject |
| __isset() | 检查属性是否已设置,即定义且不为 null。 | yii\base\BaseObject |
| __set() | 设置对象属性的值。 | yii\base\BaseObject |
| __toString() | 魔术方法,将 Cookie 对象转换为字符串,无需显式访问 $value。 | yii\web\Cookie |
| __unset() | 将对象属性设置为 null。 | yii\base\BaseObject |
| canGetProperty() | 返回一个值,指示属性是否可读。 | yii\base\BaseObject |
| canSetProperty() | 返回一个值,指示属性是否可写。 | yii\base\BaseObject |
| className() | 返回此类的完全限定名称。 | yii\base\BaseObject |
| hasMethod() | 返回一个值,指示方法是否已定义。 | yii\base\BaseObject |
| hasProperty() | 返回一个值,指示属性是否已定义。 | yii\base\BaseObject |
| init() | 初始化对象。 | yii\base\BaseObject |
常量
| 常量 | 值 | 描述 | 定义于 |
|---|---|---|---|
| SAME_SITE_LAX | 'Lax' | SameSite 策略 Lax 将阻止浏览器在所有跨站点浏览上下文中发送 Cookie,在易受 CSRF 攻击的请求方法(例如 POST、PUT、PATCH 等)期间。例如,从 https://otherdomain.com 到 https://yourdomain.com 的 POST 请求将不包含 Cookie,但 GET 请求会包含。当用户从 https://otherdomain.com 链接到 https://yourdomain.com 时,它将包含 Cookie | yii\web\Cookie |
| SAME_SITE_NONE | 'None' | SameSite 策略 None 禁用 SameSite 策略,因此 Cookie 将在所有上下文中发送,即在对第一方和跨源请求的响应中。例如,从 https://otherdomain.com 到 https://yourdomain.com 的 POST 请求将包含 Cookie。注意:如果 sameSite 设置为 None,则 secure 属性必须设置为 true(否则 Cookie 将被浏览器阻止)。 |
yii\web\Cookie |
| SAME_SITE_STRICT | 'Strict' | SameSite 策略 Strict 将阻止浏览器在所有跨站点浏览上下文中发送 Cookie,无论请求方法如何,甚至在遵循常规链接时也是如此。例如,从 https://otherdomain.com 到 https://yourdomain.com 的 GET 请求,或者用户从 https://otherdomain.com 链接到 https://yourdomain.com 将不包含 Cookie。 | yii\web\Cookie |
属性详情
Cookie 过期的时间戳或日期。这是服务器时间戳。默认为 0,表示“直到浏览器关闭”(null 也适用)。
Cookie 是否只能通过 HTTP 协议访问。通过将此属性设置为 true,Cookie 将无法被脚本语言(如 JavaScript)访问,这可以有效帮助减少通过 XSS 攻击造成的身份盗窃。
SameSite 阻止浏览器在跨站点请求中发送此 Cookie。
有关 sameSite 的更多信息,请参阅 https://mdn.org.cn/en-US/docs/Web/HTTP/Headers/Set-Cookie/SameSite。
方法详情
| public mixed __call ( $name, $params ) | ||
| $name | string |
方法名称 |
| $params | 数组 |
方法参数 |
| 返回值 | 混合类型 |
方法返回值 |
|---|---|---|
| 抛出异常 | yii\base\UnknownMethodException |
调用未知方法时 |
public function __call($name, $params)
{
throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");
}
定义于: yii\base\BaseObject::__construct()
构造函数。
默认实现执行两件事
- 使用给定的配置
$config初始化对象。 - 调用 init()。
如果此方法在子类中被重写,建议
- 构造函数的最后一个参数是一个配置数组,就像这里的
$config一样。 - 在构造函数的末尾调用父类的实现。
| public void __construct ( $config = [] ) | ||
| $config | 数组 |
将用于初始化对象属性的键值对 |
public function __construct($config = [])
{
if (!empty($config)) {
Yii::configure($this, $config);
}
$this->init();
}
定义于: yii\base\BaseObject::__get()
返回对象属性的值。
不要直接调用此方法,因为它是一个 PHP 魔术方法,当执行 $value = $object->property; 时会隐式调用。
另请参阅 __set()。
| public mixed __get ( $name ) | ||
| $name | string |
属性名称 |
| 返回值 | 混合类型 |
属性值 |
|---|---|---|
| 抛出异常 | yii\base\UnknownPropertyException |
如果属性未定义 |
| 抛出异常 | yii\base\InvalidCallException |
如果属性为只写 |
public function __get($name)
{
$getter = 'get' . $name;
if (method_exists($this, $getter)) {
return $this->$getter();
} elseif (method_exists($this, 'set' . $name)) {
throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name);
}
throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
}
定义于: yii\base\BaseObject::__isset()
检查属性是否已设置,即定义且不为 null。
不要直接调用此方法,因为它是一个 PHP 魔术方法,当执行 isset($object->property) 时会隐式调用。
请注意,如果属性未定义,则将返回 false。
| public boolean __isset ( $name ) | ||
| $name | string |
属性名称或事件名称 |
| 返回值 | boolean |
命名的属性是否已设置(非 null)。 |
|---|---|---|
public function __isset($name)
{
$getter = 'get' . $name;
if (method_exists($this, $getter)) {
return $this->$getter() !== null;
}
return false;
}
定义于: yii\base\BaseObject::__set()
设置对象属性的值。
不要直接调用此方法,因为它是一个 PHP 魔术方法,当执行 $object->property = $value; 时会隐式调用。
另请参阅 __get()。
| public void __set ( $name, $value ) | ||
| $name | string |
属性名称或事件名称 |
| $value | 混合类型 |
属性值 |
| 抛出异常 | yii\base\UnknownPropertyException |
如果属性未定义 |
|---|---|---|
| 抛出异常 | yii\base\InvalidCallException |
如果属性为只读 |
public function __set($name, $value)
{
$setter = 'set' . $name;
if (method_exists($this, $setter)) {
$this->$setter($value);
} elseif (method_exists($this, 'get' . $name)) {
throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
} else {
throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
}
}
魔术方法,将 Cookie 对象转换为字符串,无需显式访问 $value。
if (isset($request->cookies['name'])) {
$value = (string) $request->cookies['name'];
}
| public string __toString ( ) | ||
| 返回值 | string |
Cookie 的值。如果 value 属性为 null,则返回空字符串。 |
|---|---|---|
public function __toString()
{
return (string) $this->value;
}
定义于: yii\base\BaseObject::__unset()
将对象属性设置为 null。
不要直接调用此方法,因为它是一个 PHP 魔术方法,当执行 unset($object->property) 时会隐式调用。
请注意,如果属性未定义,则此方法将不执行任何操作。如果属性为只读,则会抛出异常。
| public void __unset ( $name ) | ||
| $name | string |
属性名称 |
| 抛出异常 | yii\base\InvalidCallException |
如果属性为只读。 |
|---|---|---|
public function __unset($name)
{
$setter = 'set' . $name;
if (method_exists($this, $setter)) {
$this->$setter(null);
} elseif (method_exists($this, 'get' . $name)) {
throw new InvalidCallException('Unsetting read-only property: ' . get_class($this) . '::' . $name);
}
}
定义于: yii\base\BaseObject::canGetProperty()
返回一个值,指示属性是否可读。
如果属性可读,则表示
- 类具有与指定名称关联的 getter 方法(在这种情况下,属性名称不区分大小写);
- 类具有与指定名称相同的成员变量(当
$checkVars为 true 时);
另请参阅 canSetProperty()。
| public boolean canGetProperty ( $name, $checkVars = true ) | ||
| $name | string |
属性名称 |
| $checkVars | boolean |
是否将成员变量视为属性 |
| 返回值 | boolean |
属性是否可读 |
|---|---|---|
public function canGetProperty($name, $checkVars = true)
{
return method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name);
}
定义于: yii\base\BaseObject::canSetProperty()
返回一个值,指示属性是否可写。
如果满足以下条件,则属性可写:
- 该类具有与指定名称关联的 setter 方法(在这种情况下,属性名称不区分大小写);
- 类具有与指定名称相同的成员变量(当
$checkVars为 true 时);
另请参阅 canGetProperty()。
| public boolean canSetProperty ( $name, $checkVars = true ) | ||
| $name | string |
属性名称 |
| $checkVars | boolean |
是否将成员变量视为属性 |
| 返回值 | boolean |
属性是否可写 |
|---|---|---|
public function canSetProperty($name, $checkVars = true)
{
return method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name);
}
::class 代替。
定义于: yii\base\BaseObject::className()
返回此类的完全限定名称。
| public static string className ( ) | ||
| 返回值 | string |
此类的完全限定名称。 |
|---|---|---|
public static function className()
{
return get_called_class();
}
定义于: yii\base\BaseObject::hasMethod()
返回一个值,指示方法是否已定义。
默认实现是调用 php 函数 method_exists()。当您实现 php 魔术方法 __call() 时,可以重写此方法。
| public boolean hasMethod ( $name ) | ||
| $name | string |
方法名称 |
| 返回值 | boolean |
方法是否已定义 |
|---|---|---|
public function hasMethod($name)
{
return method_exists($this, $name);
}
定义于: yii\base\BaseObject::hasProperty()
返回一个值,指示属性是否已定义。
如果满足以下条件,则属性已定义:
- 该类具有与指定名称关联的 getter 或 setter 方法(在这种情况下,属性名称不区分大小写);
- 类具有与指定名称相同的成员变量(当
$checkVars为 true 时);
另请参阅
| public boolean hasProperty ( $name, $checkVars = true ) | ||
| $name | string |
属性名称 |
| $checkVars | boolean |
是否将成员变量视为属性 |
| 返回值 | boolean |
属性是否已定义 |
|---|---|---|
public function hasProperty($name, $checkVars = true)
{
return $this->canGetProperty($name, $checkVars) || $this->canSetProperty($name, false);
}
| public void init ( ) |
public function init()
{
}
如果您由于身份验证 Cookie 导致遇到登录问题,对于 PHP 版本 < 7.3,您可以将 sameSite 属性的值设置为 None,如下所示:
**'identityCookie' => [
**'name' => 'name', 'httpOnly' => true** 'path' => '/;SameSite=None', 'secure' => true]**
对于会话 Cookie,请修改 Cookie 参数,例如:
*'cookieParams' => [
'lifetime' => time()60,
'httpOnly' => true, 'secure'=>true, 'path' => '/;SameSite=None']**
请注册或登录以发表评论。