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 $header = current($headers); 49 if (strncmp($header, 'Set-Cookie:', 11) !== 0) { 50 continue; 51 } 52 53 // If the second rolls over between the time() call and the internal time determination by 54 // setcookie(), we might get Max-Age=4 instead of Max-Age=5. 55 $header = str_replace('Max-Age=4', 'Max-Age=5', $header); 56 if ($header === current($expected)) { 57 $i--; 58 } else { 59 echo "Header mismatch:\n\tExpected: " 60 .current($expected) 61 ."\n\tReceived: ".current($headers)."\n"; 62 } 63 64 next($expected); 65} 66while (next($headers) !== FALSE); 67 68echo ($i === 0) 69 ? 'OK' 70 : 'A total of '.$i.' errors found.'; 71?> 72--EXPECTHEADERS-- 73 74--EXPECT-- 75OK 76