nystudio107 / cookies
Craft CMS 中 Twig 模板的安全 Cookie
Requires
- composer/installers: ~1.0
This package is auto-updated.
Last update: 2024-08-29 04:31:29 UTC
README
已弃用
此 Craft CMS 2.x 插件不再受支持,但它完全功能正常,您可以按需继续使用。许可协议还允许您将其分叉并进行必要的更改,以支持旧版本。
此插件的 Craft CMS 3.x 版本可在以下位置找到:craft-cookies,并且也可以通过 Craft 插件商店在 Craft CP 中安装。
Craft CMS 的 Cookies 插件
一个用于在 Craft CMS 模板中设置和获取 Cookie 的简单插件。
此插件受到 Lj_cookies 插件的启发,并具有类似的功能,但它增加了使用 craft->security() 框架获取和设置安全 Cookie 的能力,并提供 Twig 过滤器和函数以及 Craft 变量来设置和获取 Cookie。
安装
- 下载并解压缩文件,将
cookies
目录放入您的craft/plugins
目录 - -OR- 直接在
craft/plugins
文件夹中执行git clone https://github.com/khalwat/cookies.git
。然后您可以使用git pull
更新它 - 在 Craft 控制面板中的“设置”>“插件”下安装插件
- 插件文件夹应命名为
cookies
,以便 Craft 能够识别。GitHub 最近开始在 zip 文件下载的文件夹名称中附加-master
(分支名称)。
设置 Cookie
这三种方法都完成了相同的事情
{# Set the cookie using 'setCookie' function #}
{{ setCookie( NAME, VALUE, DURATION, PATH, DOMAIN, SECURE, HTTPONLY) }}
{# Set the cookie using 'setCookie' filter #}
{{ NAME | setCookie( VALUE, DURATION, PATH, DOMAIN, SECURE, HTTPONLY) }}
{# Set the cookie using 'set' variable #}
{% do craft.cookies.set( NAME, VALUE, DURATION, PATH, DOMAIN, SECURE, HTTPONLY) %}
它们都是 PHP setcookie
函数的包装器
更多信息:(https://php.ac.cn/manual/en/function.setcookie.php)
除了 NAME
之外的所有参数都是可选的。如果未指定,则 PATH
默认为 /
示例
{{ setCookie('marvin', 'martian', now | date_modify("+1 hour").timestamp ) }}
{# Sets a cookie to expire in an hour. #}
{% 'marvin' | setCookie('martian', now | date_modify("+30 days").timestamp ) %}
{# Sets a cookie to expire in 30 days. #}
{% do craft.cookies.set('marvin', 'martian', '', '/foo/' ) %}
{# Cookie available within /foo/ directory and sub-directories. #}
设置安全 Cookie
这三种方法都完成了相同的事情
{# Set the cookie using 'setSecureCookie' function #}
{{ setSecureCookie( NAME, VALUE, DURATION, PATH, DOMAIN, SECURE, HTTPONLY) }}
{# Set the cookie using 'setSecureCookie' filter #}
{{ NAME | setSecureCookie( VALUE, DURATION, PATH, DOMAIN, SECURE, HTTPONLY) }}
{# Set the cookie using 'setSecure' variable #}
{% do craft.cookies.setSecure( NAME, VALUE, DURATION, PATH, DOMAIN, SECURE, HTTPONLY) %}
此函数与 setCookie
的功能相同,但它不是使用 PHP setcookie
函数,而是使用 craft()->request->getCookies()->add
通过 Craft 添加 Cookie。它还利用 craft()->security
框架在请求之间加密和验证 Cookie 内容。
除了 NAME
之外的所有参数都是可选的。如果未指定,则 PATH
默认为 /
示例
{{ setSecureCookie('marvin', 'martian', now | date_modify("+1 hour").timestamp ) }}
{# Sets a cookie to expire in an hour. #}
{{ 'marvin' | setSecureCookie('martian', now | date_modify("+30 days").timestamp ) }}
{# Sets a cookie to expire in 30 days. #}
{% do craft.cookies.setSecure('marvin', 'martian', '', '/foo/' ) %}
{# Cookie available within /foo/ directory and sub-directories. #}
检索 Cookie
这两种方法都完成了相同的事情
{# Get the cookie using 'getCookie' function #}
{{ getCookie( NAME ) }}
{# Get the cookie using 'get' variable #}
{% do craft.cookies.get( NAME ) %}
示例
{{ getCookie('marvin') }}
{# Get the cookie using 'getCookie' function #}
{{ craft.cookies.get('marvin') }}
{# Get the cookie using 'get' variable #}
{% if getCookie('marvin') %}
{% set myCookie = getCookie('marvin') %}
{{ myCookie }}
{% endif %}
检索安全 Cookie
这两种方法都完成了相同的事情
{# Get the cookie using 'getSecureCookie' function #}
{{ getSecureCookie( NAME ) }}
{# Get the cookie using 'getSecure' variable #}
{% do craft.cookies.getSecure( NAME ) %}
示例
{{ getSecureCookie('marvin') }}
{# Get the cookie using 'getSecureCookie' function #}
{{ craft.cookies.getSecure('marvin') }}
{# Get the cookie using 'getSecure' variable #}
{% if getSecureCookie('marvin') %}
{% set myCookie = getSecureCookie('marvin') %}
{{ myCookie }}
{% endif %}
此函数与 getCookie
的功能相同,但它使用 craft()->request->getCookie()
通过 Craft 检索 Cookie。它还利用 craft()->security
框架在请求之间解密和验证 Cookie 内容。
示例
{{ getSecureCookie('marvin') }}
{# Get the cookie using 'getSecureCookie' function #}
{{ craft.cookies.getSecure('marvin') }}
{# Get the cookie using 'getSecure' variable #}
{% if getSecureCookie('marvin') %}
{% set myCookie = getSecureCookie('marvin') %}
{{ myCookie }}
{% endif %}
删除 Cookie
这三种方法都完成了相同的事情
{# Delete a cookie by passing no VALUE to 'setCookie' function #}
{{ setCookie( NAME ) }}
{# Delete a cookie by passing no VALUE to 'setCookie' filter #}
{{ NAME | setCookie() }}
{# Delete a cookie by passing no VALUE to 'set' variable #}
{% do craft.cookies.set( NAME ) %}
删除安全 Cookie
这三种方法都完成了相同的事情
{# Delete a cookie by passing no VALUE to 'setSecureCookie' function #}
{{ setSecureCookie( NAME ) }}
{# Delete a cookie by passing no VALUE to 'setSecureCookie' filter #}
{{ NAME | setSecureCookie() }}
{# Delete a cookie by passing no VALUE to 'setSecure' variable #}
{% do craft.cookies.setSecure( NAME ) %}
变更日志
1.0.4 -- 2016.04.06
- [修复] Cookie 的默认值更合理
- [改进] 更新了 README.md
1.0.3 -- 2016.03.08
- [修复] 现在如果删除 Cookie,我们将到期日期设置为过去,以强制浏览器删除它
- [改进] 更新了 README.md
1.0.2 -- 2015.11.23
- 添加了对 Craft 2.5 新插件功能的支持
- 更新了 README.md
1.0.1 -- 2015.04.25
- 添加了用于设置/获取 Cookie 的 Twig 过滤器和函数
- 将核心功能移动到服务中
- 更新了文档
1.0.0 -- 2015.04.24
- 初始发布