PHPackages                             nystudio107/cookies - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. nystudio107/cookies

ActiveCraft-plugin[Utility &amp; Helpers](/categories/utility)

nystudio107/cookies
===================

Secure Cookies for Twig Templates in Craft CMS

671415PHP

Since Jan 1Pushed 7y ago4 watchersCompare

[ Source](https://github.com/nystudio107/cookies)[ Packagist](https://packagist.org/packages/nystudio107/cookies)[ RSS](/packages/nystudio107-cookies/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (3)DependenciesVersions (1)Used By (0)

[![No Maintenance Intended](https://camo.githubusercontent.com/d904056147052e22d8e1c7f46bb50293ed2aeb4c43ead9a2d0cf7a48b46d0562/687474703a2f2f756e6d61696e7461696e65642e746563682f62616467652e737667)](http://unmaintained.tech/)

DEPRECATED
==========

[](#deprecated)

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.

The Craft CMS 3.x version of this plugin can be found here: [craft-cookies](https://github.com/nystudio107/craft-cookies) and can also be installed via the Craft Plugin Store in the Craft CP.

Cookies plugin for Craft CMS
============================

[](#cookies-plugin-for-craft-cms)

A simple plugin for setting and getting cookies from within [Craft CMS](http://buildwithcraft.com) templates.

Related: [Cookies for Craft 3.x](https://github.com/nystudio107/craft3-cookies)

This plugin is inspired the [Lj\_cookies](https://github.com/lewisjenkins/craft-lj-cookies) plugin, and functions similarly, but adds the ability to get and set secure cookies using the craft-&gt;security() framework, and it also provides Twig filters and functions as well as Craft variables for setting &amp; getting cookies.

**Installation**

1. Download &amp; unzip the file and place the `cookies` directory into your `craft/plugins` directory
2. -OR- do a `git clone https://github.com/khalwat/cookies.git` directly into your `craft/plugins` folder. You can then update it with `git pull`
3. Install plugin in the Craft Control Panel under Settings &gt; Plugins
4. The plugin folder should be named `cookies` for Craft to see it. GitHub recently started appending `-master` (the branch name) to the name of the folder for zip file downloads.

Setting cookies
---------------

[](#setting-cookies)

All three of these methods accomplish the same thing:

```
{# 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:

More info: ()

All of the parameters except for `NAME` are optional. The `PATH` defaults to `/` if not specified

**Examples**

```
{{ 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. #}

```

Setting Secure cookies
----------------------

[](#setting-secure-cookies)

All three of these methods accomplish the same thing:

```
{# 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.

All of the parameters except for `NAME` are optional. The `PATH` defaults to `/` if not specified

**Examples**

```
{{ 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. #}

```

Retrieving cookies
------------------

[](#retrieving-cookies)

Both of these methods accomplish the same thing:

```
{# Get the cookie using 'getCookie' function #}
{{ getCookie( NAME ) }}

{# Get the cookie using 'get' variable #}
{% do craft.cookies.get( NAME ) %}

```

**Example**

```
{{ 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 %}

```

Retrieving Secure cookies
-------------------------

[](#retrieving-secure-cookies)

Both of these methods accomplish the same thing:

```
{# Get the cookie using 'getSecureCookie' function #}
{{ getSecureCookie( NAME ) }}

{# Get the cookie using 'getSecure' variable #}
{% do craft.cookies.getSecure( NAME ) %}

```

**Example**

```
{{ 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.

**Example**

```
{{ 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 %}

```

Deleting cookies
----------------

[](#deleting-cookies)

All three of these methods accomplish the same thing:

```
{# 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 ) %}

```

Deleting Secure cookies
-----------------------

[](#deleting-secure-cookies)

All three of these methods accomplish the same thing:

```
{# 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 ) %}

```

Changelog
---------

[](#changelog)

### 1.0.4 -- 2016.04.06

[](#104----20160406)

- \[Fixed\] Saner default values for cookies
- \[Improved\] Updated the README.md

### 1.0.3 -- 2016.03.08

[](#103----20160308)

- \[Fixed\] We now set the expiration date to the past if we're deleting a cookie, to force browsers to remove it
- \[Improved\] Updated the README.md

### 1.0.2 -- 2015.11.23

[](#102----20151123)

- Added support for Craft 2.5 new plugin features
- Updated the README.md

### 1.0.1 -- 2015.04.25

[](#101----20150425)

- Added Twig filters and functions for setting/getting cookies
- Moved the core functionality to services
- Updated the documentation

### 1.0.0 -- 2015.04.24

[](#100----20150424)

- Initial release

###  Health Score

26

↑

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity24

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity38

Early-stage or recently created project

 Bus Factor1

Top contributor holds 92.9% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/7570798?v=4)[Andrew Welch](/maintainers/khalwat)[@khalwat](https://github.com/khalwat)

---

Top Contributors

[![khalwat](https://avatars.githubusercontent.com/u/7570798?v=4)](https://github.com/khalwat "khalwat (13 commits)")[![sweetroll](https://avatars.githubusercontent.com/u/462429?v=4)](https://github.com/sweetroll "sweetroll (1 commits)")

---

Tags

cookiescraft-plugincraft3craftcms

### Embed Badge

![Health badge](/badges/nystudio107-cookies/health.svg)

```
[![Health](https://phpackages.com/badges/nystudio107-cookies/health.svg)](https://phpackages.com/packages/nystudio107-cookies)
```

###  Alternatives

[thanks-to-it/wp-dich

121.1k](/packages/thanks-to-it-wp-dich)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
