1--TEST--
2setcookie() tests
3--INI--
4date.timezone=UTC
5--FILE--
6<?php
7setcookie('name');
8setcookie('name', '');
9setcookie('name', 'value');
10setcookie('name', 'space value');
11setcookie('name', 'value', 0);
12setcookie('name', 'value', $tsp = time() + 5);
13setcookie('name', 'value', $tsn = time() - 6);
14setcookie('name', 'value', $tsc = time());
15setcookie('name', 'value', 0, '/path/');
16setcookie('name', 'value', 0, '', 'domain.tld');
17setcookie('name', 'value', 0, '', '', TRUE);
18setcookie('name', 'value', 0, '', '', FALSE, TRUE);
19
20setcookie('name', 'value', ['expires' => $tsp]);
21setcookie('name', 'value', ['expires' => $tsn, 'path' => '/path/', 'domain' => 'domain.tld', 'secure' => true, 'httponly' => true, 'samesite' => 'Strict']);
22
23$expected = array(
24	'Set-Cookie: name=deleted; expires='.date('D, d-M-Y H:i:s', 1).' GMT; Max-Age=0',
25	'Set-Cookie: name=deleted; expires='.date('D, d-M-Y H:i:s', 1).' GMT; Max-Age=0',
26	'Set-Cookie: name=value',
27	'Set-Cookie: name=space%20value',
28	'Set-Cookie: name=value',
29	'Set-Cookie: name=value; expires='.date('D, d-M-Y H:i:s', $tsp).' GMT; Max-Age=5',
30	'Set-Cookie: name=value; expires='.date('D, d-M-Y H:i:s', $tsn).' GMT; Max-Age=0',
31	'Set-Cookie: name=value; expires='.date('D, d-M-Y H:i:s', $tsc).' GMT; Max-Age=0',
32	'Set-Cookie: name=value; path=/path/',
33	'Set-Cookie: name=value; domain=domain.tld',
34	'Set-Cookie: name=value; secure',
35	'Set-Cookie: name=value; HttpOnly',
36	'Set-Cookie: name=value; expires='.date('D, d-M-Y H:i:s', $tsp).' GMT; Max-Age=5',
37	'Set-Cookie: name=value; expires='.date('D, d-M-Y H:i:s', $tsn).' GMT; Max-Age=0; path=/path/; domain=domain.tld; secure; HttpOnly; SameSite=Strict'
38);
39
40$headers = headers_list();
41if (($i = count($expected)) > count($headers))
42{
43	echo "Fewer headers are being sent than expected - aborting";
44	return;
45}
46
47do
48{
49	if (strncmp(current($headers), 'Set-Cookie:', 11) !== 0)
50	{
51		continue;
52	}
53
54	if (current($headers) === current($expected))
55	{
56		$i--;
57	}
58	else
59	{
60		echo "Header mismatch:\n\tExpected: "
61			.current($expected)
62			."\n\tReceived: ".current($headers)."\n";
63	}
64
65	next($expected);
66}
67while (next($headers) !== FALSE);
68
69echo ($i === 0)
70	? 'OK'
71	: 'A total of '.$i.' errors found.';
72?>
73--EXPECTHEADERS--
74
75--EXPECT--
76OK
77