-
Notifications
You must be signed in to change notification settings - Fork 879
Expand file tree
/
Copy pathsetcookie.xml
More file actions
463 lines (447 loc) · 16.4 KB
/
Copy pathsetcookie.xml
File metadata and controls
463 lines (447 loc) · 16.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<refentry xml:id="function.setcookie" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<refnamediv>
<refname>setcookie</refname>
<refpurpose>Send a cookie</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type>bool</type><methodname>setcookie</methodname>
<methodparam><type>string</type><parameter>name</parameter></methodparam>
<methodparam choice="opt"><type>string</type><parameter>value</parameter><initializer>""</initializer></methodparam>
<methodparam choice="opt"><type>int</type><parameter>expires_or_options</parameter><initializer>0</initializer></methodparam>
<methodparam choice="opt"><type>string</type><parameter>path</parameter><initializer>""</initializer></methodparam>
<methodparam choice="opt"><type>string</type><parameter>domain</parameter><initializer>""</initializer></methodparam>
<methodparam choice="opt"><type>bool</type><parameter>secure</parameter><initializer>&false;</initializer></methodparam>
<methodparam choice="opt"><type>bool</type><parameter>httponly</parameter><initializer>&false;</initializer></methodparam>
</methodsynopsis>
<simpara>Alternative signature available as of PHP 7.3.0 (not supported with named parameters):</simpara>
<methodsynopsis>
<type>bool</type><methodname>setcookie</methodname>
<methodparam><type>string</type><parameter>name</parameter></methodparam>
<methodparam choice="opt"><type>string</type><parameter>value</parameter><initializer>""</initializer></methodparam>
<methodparam choice="opt"><type>array</type><parameter>options</parameter><initializer>[]</initializer></methodparam>
</methodsynopsis>
<simpara>
<function>setcookie</function> defines a cookie to be sent along with the
rest of the HTTP headers. Like other headers, cookies must be sent
<emphasis>before</emphasis> any output from the script (this is a
protocol restriction). This requires that this function is called
prior to any output, including <literal><html></literal> and
<literal><head></literal> tags as well as any whitespace.
</simpara>
<simpara>
Once the cookies have been set, they can be accessed on the next page load
with the <varname>$_COOKIE</varname> array.
Cookie
values may also exist in <varname>$_REQUEST</varname>.
</simpara>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<link xlink:href="&url.rfc;6265">RFC 6265</link> provides the normative
reference on how each <function>setcookie</function> parameter is
interpreted.
<variablelist>
<varlistentry>
<term><parameter>name</parameter></term>
<listitem>
<simpara>
The name of the cookie.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>value</parameter></term>
<listitem>
<simpara>
The value of the cookie. This value is stored on the clients computer;
do not store sensitive information. Assuming the
<parameter>name</parameter> is <literal>'cookiename'</literal>, this
value is retrieved through <varname>$_COOKIE['cookiename']</varname>
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>expires_or_options</parameter></term>
<listitem>
<simpara>
The time the cookie expires. This is a Unix timestamp so is
in number of seconds since the epoch.
One way to set this is by adding the number of seconds before the cookie
should expire to the result of calling <function>time</function>.
For instance, <literal>time()+60*60*24*30</literal> will set the cookie to
expire in 30 days.
Another option is to use the <function>mktime</function> function.
If set to <literal>0</literal>, or omitted, the cookie will expire at
the end of the session (when the browser closes).
</simpara>
<note>
<simpara>
The <parameter>expires_or_options</parameter> parameter takes a
Unix timestamp, as opposed to the date format <literal>Wdy, DD-Mon-YYYY
HH:MM:SS GMT</literal>, because PHP does this conversion
internally.
</simpara>
</note>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>path</parameter></term>
<listitem>
<simpara>
The path on the server in which the cookie will be available on.
If set to <literal>'/'</literal>, the cookie will be available
within the entire <parameter>domain</parameter>. If set to
<literal>'/foo/'</literal>, the cookie will only be available
within the <literal>/foo/</literal> directory and all
sub-directories such as <literal>/foo/bar/</literal> of
<parameter>domain</parameter>. The default value is the
current directory that the cookie is being set in.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>domain</parameter></term>
<listitem>
<simpara>
The (sub)domain that the cookie is available to. Setting this to a
subdomain (such as <literal>'www.example.com'</literal>) will make the
cookie available to that subdomain and all other sub-domains of it (i.e.
w2.www.example.com). To make the cookie available to the whole domain
(including all subdomains of it), simply set the value to the domain
name (<literal>'example.com'</literal>, in this case).
</simpara>
<simpara>
Older browsers still implementing the deprecated
<link xlink:href="&url.rfc;2109">RFC 2109</link> may require a leading
<literal>.</literal> to match all subdomains.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>secure</parameter></term>
<listitem>
<simpara>
Indicates that the cookie should only be transmitted over a
secure HTTPS connection from the client. When set to &true;, the
cookie will only be set if a secure connection exists.
On the server-side, it's on the programmer to send this
kind of cookie only on secure connection (e.g. with respect to
<varname>$_SERVER["HTTPS"]</varname>).
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>httponly</parameter></term>
<listitem>
<simpara>
When &true; the cookie will be made accessible only through the HTTP
protocol. This means that the cookie won't be accessible by
scripting languages, such as JavaScript. It has been suggested that
this setting can effectively help to reduce identity theft through
XSS attacks (although it is not supported by all browsers), but that
claim is often disputed.
&true; or &false;
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>options</parameter></term>
<listitem>
<simpara>
An associative <type>array</type> which may have any of the keys
<literal>expires</literal>, <literal>path</literal>, <literal>domain</literal>,
<literal>secure</literal>, <literal>httponly</literal> and <literal>samesite</literal>.
</simpara>
<simpara>
The values have the same meaning as described for the
parameters with the same name. The value of the <literal>samesite</literal>
element should be either <literal>None</literal>, <literal>Lax</literal>
or <literal>Strict</literal>.
If any of the allowed options are not given, their default values are the
same as the default values of the explicit parameters. If the
<literal>samesite</literal> element is omitted, no SameSite cookie
attribute is set.
</simpara>
<note>
<simpara>
To set a cookie that includes attributes that aren't among the keys listed,
use <function>header</function>.
</simpara>
</note>
<note>
<simpara>
If <literal>samesite</literal> is <literal>"None"</literal> then
<literal>secure</literal> must also be enabled or the cookie will be
blocked by the client.
</simpara>
</note>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<simpara>
If output exists prior to calling this function,
<function>setcookie</function> will fail and return &false;. If
<function>setcookie</function> successfully runs, it will return &true;.
This does not indicate whether the user accepted the cookie.
</simpara>
</refsect1>
<refsect1 role="errors">
&reftitle.errors;
<simpara>
If the <parameter>options</parameter> array contains unsupported keys:
</simpara>
<itemizedlist>
<listitem>
<simpara>
Prior to PHP 8.0.0, an <constant>E_WARNING</constant> was generated.
</simpara>
</listitem>
<listitem>
<simpara>
As of PHP 8.0.0, a <exceptionname>ValueError</exceptionname> is thrown.
</simpara>
</listitem>
</itemizedlist>
</refsect1>
<refsect1 role="changelog">
&reftitle.changelog;
<informaltable>
<tgroup cols="2">
<thead>
<row>
<entry>&Version;</entry>
<entry>&Description;</entry>
</row>
</thead>
<tbody>
<row>
<entry>8.2.0</entry>
<entry>
The date format of the cookie is now <literal>'D, d M Y H:i:s \G\M\T'</literal>;
previously it was <literal>'D, d-M-Y H:i:s T'</literal>.
</entry>
</row>
<row>
<entry>8.0.0</entry>
<entry>
Passing unsupported keys now throws a <exceptionname>ValueError</exceptionname>
instead of emitting an <constant>E_WARNING</constant>.
</entry>
</row>
<row>
<entry>7.3.0</entry>
<entry>
An alternative signature supporting an <parameter>options</parameter>
array has been added. This signature supports also setting of the
SameSite cookie attribute.
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<simpara>
The effects of the following examples can be observed using the browser
developer tools cookie list (usually in the Storage or Application tab).
</simpara>
<example>
<title><function>setcookie</function> send example</title>
<programlisting role="php">
<![CDATA[
<?php
$value = 'something from somewhere';
// Set a "session cookie" that expires when the browser is closed
setcookie("TestCookie", $value);
// Set a cookie that expires in 1 hour
setcookie("TestCookie", $value, time()+3600);
// Set a cookie that applies only to a specific path on a specific domain
// Note that the domain used should match the site domain
setcookie("TestCookie", $value, time()+3600, "/~rasmus/", "example.com", true);
?>
]]>
</programlisting>
</example>
<simpara>
Note that the value portion of the cookie will automatically be
urlencoded and decoded by PHP. This can be avoided by using
<function>setrawcookie</function> instead.
</simpara>
<simpara>
To see the contents of the cookies set in the above example on a later
request:
</simpara>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
// Print an individual cookie
echo $_COOKIE["TestCookie"];
// Another way to debug/test is to view all cookies
print_r($_COOKIE);
?>
]]>
</programlisting>
</informalexample>
<example>
<title><function>setcookie</function> delete example</title>
<simpara>
To delete a cookie set the expiration date to a value in the past
(but not zero, which is reserved for session cookies).
</simpara>
<simpara>
To delete the cookies set in previous example:
</simpara>
<programlisting role="php">
<![CDATA[
<?php
// set the expiration date to one hour ago
setcookie("TestCookie", "", time() - 3600);
setcookie("TestCookie", "", time() - 3600, "/~rasmus/", "example.com", 1);
?>
]]>
</programlisting>
</example>
<example>
<title><function>setcookie</function> and arrays</title>
<simpara>
An "array of cookies" can be set by using array notation in the
cookie name. This has the effect of setting as many cookies as
there are array elements, but when the cookie is received by the
script, the values are all placed in an array with the cookie's
name:
</simpara>
<programlisting role="php">
<![CDATA[
<?php
// set the cookies
setcookie("cookie[three]", "cookiethree");
setcookie("cookie[two]", "cookietwo");
setcookie("cookie[one]", "cookieone");
// after the page reloads, print them out
if (isset($_COOKIE['cookie'])) {
foreach ($_COOKIE['cookie'] as $name => $value) {
$name = htmlspecialchars($name);
$value = htmlspecialchars($value);
echo "$name : $value <br />\n";
}
}
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
three : cookiethree
two : cookietwo
one : cookieone
]]>
</screen>
</example>
<note>
<simpara>
Using separator characters such as <literal>[</literal> and <literal>]</literal>
as part of the cookie name is not compliant to RFC 6265, section 4, but supposed
to be supported by user agents according to RFC 6265, section 5.
</simpara>
</note>
</refsect1>
<refsect1 role="notes">
&reftitle.notes;
<note>
<simpara>
Output buffering can be used to allow script output prior to the
call of this function. All output will be buffered until it is flushed
(either explicitly or at the end of the script execution). This is done
by calling <function>ob_start</function> and
<function>ob_end_flush</function> in the script, or setting the
<literal>output_buffering</literal> configuration directive on in the
&php.ini; or server configuration files.
</simpara>
</note>
<para>
Common Pitfalls:
<itemizedlist>
<listitem>
<simpara>
Cookies will not become visible until the next loading of a page that
the cookie should be visible for. To test if a cookie was successfully
set, check for the cookie on a next loading page before the cookie
expires. Expire time is set via the <parameter>expires_or_options</parameter>
parameter. A nice way to debug the existence of cookies is by
simply calling <literal>print_r($_COOKIE);</literal>.
</simpara>
</listitem>
<listitem>
<simpara>
Cookies must be deleted with the same parameters as they were set with.
If the <parameter>value</parameter> argument is an empty string, and all other arguments
match a previous call to <function>setcookie</function>, then the cookie with the specified
name will be deleted from the remote client.
This is internally achieved by setting value to <literal>'deleted'</literal> and expiration
time in the past.
</simpara>
</listitem>
<listitem>
<simpara>
Because setting a cookie with a value of &false; will try to delete the cookie,
boolean values should not be used. Instead, use <emphasis>0</emphasis> for &false;
and <emphasis>1</emphasis> for &true;.
</simpara>
</listitem>
<listitem>
<simpara>
Cookies names can be set as array names and will be available to the
PHP scripts as arrays but separate cookies are stored by the browser.
Consider <function>json_encode</function> to set one cookie with multiple
names and values. It is not recommended to use <function>serialize</function>
for this purpose because it can result in security holes.
</simpara>
</listitem>
</itemizedlist>
</para>
<simpara>
Multiple calls to <function>setcookie</function> are performed in the order called.
</simpara>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<simplelist>
<member><function>header</function></member>
<member><function>setrawcookie</function></member>
<member><link linkend="features.cookies">cookies section</link></member>
<member><link xlink:href="&url.rfc;6265">RFC 6265</link></member>
<member><link xlink:href="&url.rfc;2109">RFC 2109</link></member>
</simplelist>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->