CakePHP
  • 文档
    • 手册
    • API
    • 视频
    • 报告安全问题
    • 隐私政策
    • 标识和商标
  • 商业解决方案
  • 商品
  • 路途
  • 团队
  • 社区
    • 社区
    • 参与进来
    • 问题 (Github)
    • Bakery
    • 精选资源
    • 培训
    • 聚会
    • 我的 CakePHP
    • CakeFest
    • 新闻简报
    • 领英
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • 帮助和支持
    • 论坛
    • Stack Overflow
    • IRC
    • Slack
    • 付费支持
CakePHP

C CakePHP 5.1 Chiffon API

  • 项目
    • CakePHP
      • CakePHP
      • Chronos
      • Elastic Search
      • 队列
  • 版本
    • 5.1
      • 5.1
      • 5.0
      • 4.5
      • 4.4
      • 4.3
      • 4.2
      • 4.1
      • 4.0
      • 3.10
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

命名空间

  • 全局
  • Cake
    • 缓存
    • 集合
    • 命令
    • 控制台
    • 控制器
    • 核心
    • 数据库
    • 数据源
    • 错误
    • 事件
    • 表单
    • Http
      • 客户端
      • Cookie
      • 异常
      • 中间件
      • 会话
      • 测试套件
    • I18n
    • 日志
    • 邮件
    • 网络
    • ORM
    • 路由
    • 测试套件
    • 工具
    • 验证
    • 视图

Cookie 类

用于构建 Cookie 并将其转换为标头值的 Cookie 对象。

HTTP Cookie(也称为 Web Cookie、Internet Cookie、浏览器 Cookie 或简称为 Cookie)是网站发送并由用户浏览器存储在用户计算机上的少量数据,在用户浏览时使用。

Cookie 的设计目的是为网站提供可靠的机制来记住有状态信息(例如在线商店中添加的商品)或记录用户的浏览活动(包括单击特定按钮、登录或记录过去访问的页面)。它们还可以用于记住用户以前在表单字段中输入的任意信息,例如姓名和首选项。

Cookie 对象是不可变的,修改 Cookie 对象时必须重新分配变量。

$cookie = $cookie->withValue('0');
命名空间: Cake\Http\Cookie
参见: \Cake\Http\Cookie\CookieCollection 用于处理 Cookie 集合。
参见: \Cake\Http\Response::getCookieCollection() 用于处理响应 Cookie。
链接: https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-03
链接: https://en.wikipedia.org/wiki/HTTP_cookie

常量

  • string
    EXPIRES_FORMAT ¶
    'D, d-M-Y H:i:s T'

    过期属性格式。

  • string
    SAMESITE_LAX ¶
    'Lax'

    SameSite 属性值:Lax

  • string
    SAMESITE_NONE ¶
    'None'

    SameSite 属性值:None

  • string
    SAMESITE_STRICT ¶
    'Strict'

    SameSite 属性值:Strict

  • list<string>
    SAMESITE_VALUES ¶
    [self::SAMESITE_LAX, self::SAMESITE_STRICT, self::SAMESITE_NONE]

    "SameSite" 属性的有效值。

属性摘要

  • $defaults protected static
    array<string, mixed>

    Cookie 的默认属性。

  • $domain protected
    string

    域名

  • $expiresAt protected
    DateTimeInterface|null

    过期时间

  • $httpOnly protected
    bool

    HTTP 仅

  • $isExpanded protected
    bool

    JSON 值是否已扩展为数组。

  • $name protected
    string

    Cookie 名称

  • $path protected
    string

    路径

  • $sameSite protected
    Cake\Http\Cookie\SameSiteEnum|null

    Samesite

  • $secure protected
    bool

    安全

  • $value protected
    array|string

    原始 Cookie 值。

方法摘要

  • __construct() public

    构造函数

  • _expand() protected

    展开方法,从 CookieComponent::_flatten() 中设置的字符串返回数组,保持与 1.x CookieComponent::_flatten() 的向后兼容性。

  • _flatten() protected

    合并方法以保留多维数组的键。

  • _setValue() protected

    值属性的设置器。

  • check() public

    检查 Cookie 数据中是否存在值。

  • create() public static

    创建 Cookie 实例的工厂方法。

  • createFromHeaderString() public static

    从 "set-cookie" 标头字符串创建 Cookie 实例。

  • dateTimeInstance() protected static

    将非空过期值转换为 DateTimeInterface 实例。

  • getDomain() public

    获取域名属性。

  • getExpiresTimestamp() public

    从过期时间获取时间戳。

  • getExpiry() public

    获取当前过期时间。

  • getFormattedExpires() public

    构建标头字符串的过期值部分。

  • getId() public

    获取 Cookie 的 ID。

  • getName() public

    获取 Cookie 名称。

  • getOptions() public

    获取 Cookie 选项。

  • getPath() public

    获取路径属性。

  • getSameSite() public

    获取 SameSite 属性。

  • getScalarValue() public

    以标量形式获取 Cookie 值。

  • getValue() public

    获取 Cookie 值。

  • isExpanded() public

    检查 Cookie 值是否已扩展。

  • isExpired() public

    检查 Cookie 与 $time 相比是否已过期。

  • isHttpOnly() public

    检查 Cookie 是否为 HTTP 仅。

  • isSecure() public

    检查 Cookie 是否安全。

  • read() public

    从 Cookie 读取数据。

  • resolveSameSiteEnum() protected static

    创建 SameSiteEnum 实例。

  • setDefaults() public static

    设置 Cookie 的默认选项。

  • toArray() public

    以数组形式获取 Cookie 数据。

  • toHeaderValue() public

    以字符串形式返回标头值。

  • validateName() protected

    验证 Cookie 名称。

  • withAddedValue() public

    创建一个具有更新数据的全新 Cookie。

  • withDomain() public

    创建一个具有更新域名的 Cookie。

  • withExpired() public

    创建一个将过期/从浏览器中删除 Cookie 的全新 Cookie。

  • withExpiry() public

    创建一个具有更新过期日期的 Cookie。

  • withHttpOnly() public

    创建一个具有更新的 HTTP 仅的 Cookie。

  • withName() public

    设置 Cookie 名称。

  • withNeverExpire() public

    创建一个实际上永远不会过期的全新 Cookie。

  • withPath() public

    创建一个具有更新路径的新 cookie

  • withSameSite() public

    创建一个具有更新 SameSite 选项的 cookie。

  • withSecure() public

    创建一个具有 Secure 更新的 cookie

  • withValue() public

    创建一个具有更新值的 cookie。

  • withoutAddedValue() public

    创建一个没有特定路径的新 cookie

方法详情

__construct() ¶ public

__construct(string $name, array|string|float|int|bool $value = '', DateTimeInterface|null $expiresAt = null, string|null $path = null, string|null $domain = null, bool|null $secure = null, bool|null $httpOnly = null, Cake\Http\Cookie\SameSiteEnum|string|null $sameSite = null)

构造函数

构造函数的参数类似于本机 PHP setcookie() 方法。唯一的区别是第三个参数,它期望传入 null 或 DateTime 或 DateTimeImmutable 对象,而不是整数。

参数
string $name

Cookie 名称

array|string|float|int|bool $value optional

cookie 的值

DateTimeInterface|null $expiresAt optional

过期时间和日期

string|null $path optional

路径

string|null $domain optional

域名

bool|null $secure optional

是否安全

bool|null $httpOnly optional

HTTP Only

Cake\Http\Cookie\SameSiteEnum|string|null $sameSite optional

Samesite

链接
https://php.ac.cn/manual/en/function.setcookie.php

_expand() ¶ protected

_expand(string $string): array|string

展开方法,从 CookieComponent::_flatten() 中设置的字符串返回数组,保持与 1.x CookieComponent::_flatten() 的向后兼容性。

参数
string $string

包含 JSON 编码数据的字符串,或一个简单的字符串。

返回值
array|string

_flatten() ¶ protected

_flatten(array $array): string

合并方法以保留多维数组的键。

参数
array $array

键值对映射

返回值
string

_setValue() ¶ protected

_setValue(array|string|float|int|bool $value): void

值属性的设置器。

参数
array|string|float|int|bool $value

要存储的值。

返回值
void

check() ¶ public

check(string $path): bool

检查 Cookie 数据中是否存在值。

此方法将在首次使用时扩展序列化后的复杂数据。

参数
string $path

要检查的路径

返回值
bool

create() ¶ public static

create(string $name, array|string|float|int|bool $value, array<string, mixed> $options = []): static

创建 Cookie 实例的工厂方法。

参数
string $name

Cookie 名称

array|string|float|int|bool $value

cookie 的值

array<string, mixed> $options optional

Cookie 选项。

返回值
static
参见
\Cake\Cookie\Cookie::setDefaults()

createFromHeaderString() ¶ public static

createFromHeaderString(string $cookie, array<string, mixed> $defaults = []): static

从 "set-cookie" 标头字符串创建 Cookie 实例。

参数
string $cookie

Cookie 头部字符串。

array<string, mixed> $defaults optional

默认属性。

返回值
static
参见
\Cake\Http\Cookie\Cookie::setDefaults()

dateTimeInstance() ¶ protected static

dateTimeInstance(DateTimeInterface|string|int|null $expires): DateTimeInterface|null

将非空过期值转换为 DateTimeInterface 实例。

参数
DateTimeInterface|string|int|null $expires

过期值。

返回值
DateTimeInterface|null

getDomain() ¶ public

getDomain(): string

获取域名属性。

返回值
string

getExpiresTimestamp() ¶ public

getExpiresTimestamp(): int|null

从过期时间获取时间戳。

返回值
int|null

getExpiry() ¶ public

getExpiry(): DateTimeInterface|null

获取当前过期时间。

返回值
DateTimeInterface|null

getFormattedExpires() ¶ public

getFormattedExpires(): string

构建标头字符串的过期值部分。

返回值
string

getId() ¶ public

getId(): string

获取 Cookie 的 ID。

Cookie 在名称、域名和路径元组上是唯一的。

返回值
string

getName() ¶ public

getName(): string

获取 Cookie 名称。

返回值
string

getOptions() ¶ public

getOptions(): array<string, mixed>

获取 Cookie 选项。

返回值
array<string, mixed>

getPath() ¶ public

getPath(): string

获取路径属性。

返回值
string

getSameSite() ¶ public

getSameSite(): Cake\Http\Cookie\SameSiteEnum|null

获取 SameSite 属性。

返回值
Cake\Http\Cookie\SameSiteEnum|null

getScalarValue() ¶ public

getScalarValue(): string

以标量形式获取 Cookie 值。

这将使用 json_encode() 折叠 cookie 中的所有复杂数据

返回值
string

getValue() ¶ public

getValue(): array|string

获取 Cookie 值。

返回值
array|string

isExpanded() ¶ public

isExpanded(): bool

检查 Cookie 值是否已扩展。

返回值
bool

isExpired() ¶ public

isExpired(DateTimeInterface|null $time = null): bool

检查 Cookie 与 $time 相比是否已过期。

没有过期日期的 cookie 始终返回 false。

参数
DateTimeInterface|null $time optional
返回值
bool

isHttpOnly() ¶ public

isHttpOnly(): bool

检查 Cookie 是否为 HTTP 仅。

返回值
bool

isSecure() ¶ public

isSecure(): bool

检查 Cookie 是否安全。

返回值
bool

read() ¶ public

read(string|null $path = null): mixed

从 Cookie 读取数据。

此方法将在首次使用时扩展序列化后的复杂数据。

参数
string|null $path optional

要读取数据的路径

返回值
mixed

resolveSameSiteEnum() ¶ protected static

resolveSameSiteEnum(Cake\Http\Cookie\SameSiteEnum|string|null $sameSite): Cake\Http\Cookie\SameSiteEnum|null

创建 SameSiteEnum 实例。

参数
Cake\Http\Cookie\SameSiteEnum|string|null $sameSite

SameSite 值

返回值
Cake\Http\Cookie\SameSiteEnum|null

setDefaults() ¶ public static

setDefaults(array<string, mixed> $options): void

设置 Cookie 的默认选项。

有效的选项键是

  • expires: 可以是 UNIX 时间戳或 strtotime() 兼容的字符串或 DateTimeInterface 实例或 null。
  • path: 路径字符串。默认为 '/'。
  • domain: 域名字符串。默认为 ''。
  • httponly: 布尔值。默认为 false。
  • secure: 布尔值。默认为 false。
  • samesite: 可以是 CookieInterface::SAMESITE_LAX、CookieInterface::SAMESITE_STRICT、CookieInterface::SAMESITE_NONE 或 null 之一。默认为 null。
参数
array<string, mixed> $options

默认选项。

返回值
void

toArray() ¶ public

toArray(): array<string, mixed>

以数组形式获取 Cookie 数据。

返回值
array<string, mixed>

toHeaderValue() ¶ public

toHeaderValue(): string

以字符串形式返回标头值。

返回值
string

validateName() ¶ protected

validateName(string $name): void

验证 Cookie 名称。

参数
string $name

cookie 的名称

返回值
void
抛出
InvalidArgumentException
链接
https://tools.ietf.org/html/rfc2616#section-2.2 命名 cookie 的规则。

withAddedValue() ¶ public

withAddedValue(string $path, mixed $value): static

创建一个具有更新数据的全新 Cookie。

参数
string $path

要写入的路径

mixed $value

要写入的值

返回值
static

withDomain() ¶ public

withDomain(string $domain): static

创建一个具有更新域名的 Cookie。

参数
string $domain
返回值
static

withExpired() ¶ public

withExpired(): static

创建一个将过期/从浏览器中删除 Cookie 的全新 Cookie。

这是通过将过期时间设置为一年前来完成的。

返回值
static

withExpiry() ¶ public

withExpiry(DateTimeInterface $dateTime): static

创建一个具有更新过期日期的 Cookie。

参数
DateTimeInterface $dateTime
返回值
static

withHttpOnly() ¶ public

withHttpOnly(bool $httpOnly): static

创建一个具有更新的 HTTP 仅的 Cookie。

参数
bool $httpOnly
返回值
static

withName() ¶ public

withName(string $name): static

设置 Cookie 名称。

参数
string $name
返回值
static

withNeverExpire() ¶ public

withNeverExpire(): static

创建一个实际上永远不会过期的全新 Cookie。

返回值
static

withPath() ¶ public

withPath(string $path): static

创建一个具有更新路径的新 cookie

参数
string $path
返回值
static

withSameSite() ¶ public

withSameSite(Cake\Http\Cookie\SameSiteEnum|string|null $sameSite): static

创建一个具有更新 SameSite 选项的 cookie。

参数
Cake\Http\Cookie\SameSiteEnum|string|null $sameSite
返回值
static

withSecure() ¶ public

withSecure(bool $secure): static

创建一个具有 Secure 更新的 cookie

参数
bool $secure
返回值
static

withValue() ¶ public

withValue(array|string|float|int|bool $value): static

创建一个具有更新值的 cookie。

参数
array|string|float|int|bool $value
返回值
static

withoutAddedValue() ¶ public

withoutAddedValue(string $path): static

创建一个没有特定路径的新 cookie

参数
string $path

要移除的路径。

返回值
static

属性详情

$defaults ¶ protected static

Cookie 的默认属性。

类型
array<string, mixed>

$domain ¶ protected

域名

类型
string

$expiresAt ¶ protected

过期时间

类型
DateTimeInterface|null

$httpOnly ¶ protected

HTTP 仅

类型
bool

$isExpanded ¶ protected

JSON 值是否已扩展为数组。

类型
bool

$name ¶ protected

Cookie 名称

类型
string

$path ¶ protected

路径

类型
string

$sameSite ¶ protected

Samesite

类型
Cake\Http\Cookie\SameSiteEnum|null

$secure ¶ protected

安全

类型
bool

$value ¶ protected

原始 Cookie 值。

类型
array|string
OpenHub
Pingping
Linode
  • 商业解决方案
  • 展示
  • 文档
  • 手册
  • API
  • 视频
  • 报告安全问题
  • 隐私政策
  • 标识和商标
  • 社区
  • 参与进来
  • 问题 (Github)
  • Bakery
  • 精选资源
  • 培训
  • 聚会
  • 我的 CakePHP
  • CakeFest
  • 新闻简报
  • 领英
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • 帮助和支持
  • 论坛
  • Stack Overflow
  • IRC
  • Slack
  • 付费支持

使用 CakePHP API 文档 生成。