類別 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 將阻止瀏覽器在所有跨站瀏覽情境中,於容易發生 CSRF 的請求方法(例如 POST、PUT、PATCH 等)期間傳送 Cookie。例如,從 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
也適用)。
指出是否應僅透過 HTTP 協定存取 Cookie。將此屬性設定為 true,Cookie 將無法被腳本語言(例如 JavaScript)存取,這可以有效地幫助減少透過 XSS 攻擊的身分盜竊。
SameSite 防止瀏覽器在跨站請求中傳送此 Cookie。
有關 sameSite 的更多資訊,請參閱 https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie/SameSite。
方法詳情
public mixed __call ( $name, $params ) | ||
$name | string |
方法名稱 |
$params | array |
方法參數 |
return | mixed |
方法傳回值 |
---|---|---|
throws | 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 | array |
將用於初始化物件屬性的名稱-值對 |
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 |
屬性名稱 |
return | mixed |
屬性值 |
---|---|---|
throws | yii\base\UnknownPropertyException |
如果未定義屬性 |
throws | 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 |
屬性名稱或事件名稱 |
return | 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 | mixed |
屬性值 |
throws | yii\base\UnknownPropertyException |
如果未定義屬性 |
---|---|---|
throws | 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 ( ) | ||
return | 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 |
屬性名稱 |
throws | 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 |
是否將成員變數視為屬性 |
return | 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 |
是否將成員變數視為屬性 |
return | 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 ( ) | ||
return | string |
此類別的完整限定名稱。 |
---|
public static function className()
{
return get_called_class();
}
定義於: yii\base\BaseObject::hasMethod()
傳回一個值,指示是否已定義方法。
預設實作是對 php 函數 method_exists()
的呼叫。當您實作 php 魔術方法 __call()
時,您可以覆寫此方法。
public boolean hasMethod ( $name ) | ||
$name | string |
方法名稱 |
return | 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 |
是否將成員變數視為屬性 |
return | boolean |
屬性是否已定義 |
---|
public function hasProperty($name, $checkVars = true)
{
return $this->canGetProperty($name, $checkVars) || $this->canSetProperty($name, false);
}
public void init ( ) |
public function init()
{
}
如果您因為 Identify Cookies 而面臨登入問題,對於 PHP 版本 < 7.3,您可以將 sameSite 屬性值設定為 None,如下所示:
**'identityCookie' => [
**'name' => 'name', 'httpOnly' => true** 'path' => '/;SameSite=None', 'secure' => true
]**
對於 session cookie,修改 cookie 參數,如下所示:
*'cookieParams' => [
'lifetime' => time()60,
'httpOnly' => true, 'secure'=>true, 'path' => '/;SameSite=None'
]**
註冊 或 登入 以發表評論。