You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
They all act as a wrapper for the PHP `setcookie` function. [More info](http://php.net/manual/en/function.setcookie.php)
36
38
37
-
All of the parameters except for `NAME` are optional. The `PATH` defaults to `/` if not specified
39
+
All of the parameters except for `NAME` are optional. The `PATH` defaults to `/` if not specified. The `SAMESITE` should be either 'None', 'Lax' or 'Strict'.
40
+
41
+
(Note: `SAMESITE` only works for environments with PHP 7.3 and up)
38
42
39
43
**Examples**
44
+
40
45
```twig
41
46
{% do setCookie('marvin', 'martian', now | date_modify("+1 hour").timestamp) %}
42
47
{# Sets a cookie to expire in an hour. #}
@@ -47,24 +52,30 @@ All of the parameters except for `NAME` are optional. The `PATH` defaults to `/
47
52
{% do craft.cookies.set('marvin', 'martian', '', '/foo/' ) %}
48
53
{# Cookie available within /foo/ directory and sub-directories. #}
49
54
```
55
+
50
56
## Setting Secure cookies
51
57
52
58
All three of these methods accomplish the same thing:
59
+
53
60
```twig
54
61
{# Set the cookie using 'setSecureCookie' function #}
This function works the same as `setCookie` but instead of using the PHP `setcookie` function, it uses the `Craft::$app->getResponse()->getCookies()->add` to add the cookies via Craft. It also utilizes `craft->security` framework to encrypt and validate the cookie contents between requests.
64
70
65
-
All of the parameters except for `NAME` are optional. The `PATH` defaults to `/` if not specified
71
+
This function works the same as `setCookie` but instead of using the PHP `setcookie` function, it uses the `Craft::$app->getResponse()->getCookies()->add` to add the cookies via Craft. It also utilizes `craft->security` framework to encrypt and validate the cookie contents between requests.
72
+
73
+
All of the parameters except for `NAME` are optional. The `PATH` defaults to `/` if not specified. The `SAMESITE` should be either 'None', 'Lax' or 'Strict'.
74
+
75
+
(Note: `SAMESITE` only works for environments with PHP 7.3 and up)
66
76
67
77
**Examples**
78
+
68
79
```twig
69
80
{% do setSecureCookie('marvin', 'martian', now | date_modify("+1 hour").timestamp) %}
70
81
{# Sets a cookie to expire in an hour. #}
@@ -75,17 +86,21 @@ All of the parameters except for `NAME` are optional. The `PATH` defaults to `/
75
86
{% do craft.cookies.setSecure('marvin', 'martian', '', '/foo/' ) %}
76
87
{# Cookie available within /foo/ directory and sub-directories. #}
77
88
```
89
+
78
90
## Retrieving cookies
79
91
80
92
Both of these methods accomplish the same thing:
93
+
81
94
```twig
82
95
{# Get the cookie using 'getCookie' function #}
83
96
{% do getCookie( NAME ) %}
84
97
85
98
{# Get the cookie using 'get' variable #}
86
99
{% do craft.cookies.get( NAME ) %}
87
100
```
101
+
88
102
**Example**
103
+
89
104
```twig
90
105
{% do getCookie('marvin') %}
91
106
{# Get the cookie using 'getCookie' function #}
@@ -98,17 +113,21 @@ Both of these methods accomplish the same thing:
98
113
{{ myCookie }}
99
114
{% endif %}
100
115
```
116
+
101
117
## Retrieving Secure cookies
102
118
103
119
Both of these methods accomplish the same thing:
120
+
104
121
```twig
105
122
{# Get the cookie using 'getSecureCookie' function #}
106
123
{% do getSecureCookie( NAME ) %}
107
124
108
125
{# Get the cookie using 'getSecure' variable #}
109
126
{% do craft.cookies.getSecure( NAME ) %}
110
127
```
128
+
111
129
**Example**
130
+
112
131
```twig
113
132
{% do getSecureCookie('marvin') %}
114
133
{# Get the cookie using 'getSecureCookie' function #}
@@ -121,9 +140,11 @@ Both of these methods accomplish the same thing:
121
140
{{ myCookie }}
122
141
{% endif %}
123
142
```
124
-
This function works the same as `getCookie` but it uses `Craft::$app->getRequest()->getCookie()` to retrieve the cookies via Craft. It also utilizes `craft->security` framework to decrypt and validate the cookie contents between requests.
143
+
144
+
This function works the same as `getCookie` but it uses `Craft::$app->getRequest()->getCookie()` to retrieve the cookies via Craft. It also utilizes `craft->security` framework to decrypt and validate the cookie contents between requests.
125
145
126
146
**Example**
147
+
127
148
```twig
128
149
{% do getSecureCookie('marvin') %}
129
150
{# Get the cookie using 'getSecureCookie' function #}
@@ -136,9 +157,11 @@ This function works the same as `getCookie` but it uses `Craft::$app->getRequest
136
157
{{ myCookie }}
137
158
{% endif %}
138
159
```
160
+
139
161
## Deleting cookies
140
162
141
163
All three of these methods accomplish the same thing:
164
+
142
165
```twig
143
166
{# Delete a cookie by passing no VALUE to 'setCookie' function #}
144
167
{% do setCookie( NAME ) %}
@@ -149,9 +172,11 @@ All three of these methods accomplish the same thing:
149
172
{# Delete a cookie by passing no VALUE to 'set' variable #}
150
173
{% do craft.cookies.set( NAME ) %}
151
174
```
175
+
152
176
## Deleting Secure cookies
153
177
154
178
All three of these methods accomplish the same thing:
179
+
155
180
```twig
156
181
{# Delete a cookie by passing no VALUE to 'setSecureCookie' function #}
157
182
{% do setSecureCookie( NAME ) %}
@@ -162,4 +187,5 @@ All three of these methods accomplish the same thing:
162
187
{# Delete a cookie by passing no VALUE to 'setSecure' variable #}
163
188
{% do craft.cookies.setSecure( NAME ) %}
164
189
```
190
+
165
191
Brought to you by [nystudio107](http://nystudio107.com)
0 commit comments