Wallogit.com
2017 © Pedro Peláez
Secure Cookies for Twig Templates in Craft CMS
This Craft CMS 2.x plugin is no longer supported, but it is fully functional, and you may continue to use it as you see fit. The license also allows you to fork it and make changes as needed for legacy support reasons., (*2)
The Craft CMS 3.x version of this plugin can be found here: craft-cookies and can also be installed via the Craft Plugin Store in the Craft CP., (*3)
A simple plugin for setting and getting cookies from within Craft CMS templates., (*4)
Related: Cookies for Craft 3.x, (*5)
This plugin is inspired the Lj_cookies plugin, and functions similarly, but adds the ability to get and set secure cookies using the craft->security() framework, and it also provides Twig filters and functions as well as Craft variables for setting & getting cookies., (*6)
Installation, (*7)
cookies directory into your craft/plugins directorygit clone https://github.com/khalwat/cookies.git directly into your craft/plugins folder. You can then update it with git pull
cookies for Craft to see it. GitHub recently started appending -master (the branch name) to the name of the folder for zip file downloads.All three of these methods accomplish the same thing:, (*8)
{# 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) %}
They all act as a wrapper for the PHP setcookie function:, (*9)
More info: (http://php.net/manual/en/function.setcookie.php), (*10)
All of the parameters except for NAME are optional. The PATH defaults to / if not specified, (*11)
Examples, (*12)
{{ 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. #}
All three of these methods accomplish the same thing:, (*13)
{# 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) %}
This function works the same as setCookie but instead of using the PHP setcookie function, it uses the craft()->request->getCookies()->add to add the cookies via Craft. It also utilizes craft->security framework to encrypt and validate the cookie contents between requests., (*14)
All of the parameters except for NAME are optional. The PATH defaults to / if not specified, (*15)
Examples, (*16)
{{ 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. #}
Both of these methods accomplish the same thing:, (*17)
{# Get the cookie using 'getCookie' function #}
{{ getCookie( NAME ) }}
{# Get the cookie using 'get' variable #}
{% do craft.cookies.get( NAME ) %}
Example, (*18)
{{ 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 %}
Both of these methods accomplish the same thing:, (*19)
{# Get the cookie using 'getSecureCookie' function #}
{{ getSecureCookie( NAME ) }}
{# Get the cookie using 'getSecure' variable #}
{% do craft.cookies.getSecure( NAME ) %}
Example, (*20)
{{ 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 %}
This function works the same as getCookie but it uses craft()->request->getCookie() to retrieve the cookies via Craft. It also utilizes craft->security framework to decrypt and validate the cookie contents between requests., (*21)
Example, (*22)
{{ 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 %}
All three of these methods accomplish the same thing:, (*23)
{# 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 ) %}
All three of these methods accomplish the same thing:, (*24)
{# 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 ) %}