Skip to content

Commit 742b0e5

Browse files
committed
Merge branch 'PHP-5.5' of git.php.net:php-src into PHP-5.5
2 parents cbb5d42 + ec2fff8 commit 742b0e5

File tree

4 files changed

+87
-7
lines changed

4 files changed

+87
-7
lines changed

NEWS

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ PHP NEWS
77
. Fixed bug #63822 (Crash when using closures with ArrayAccess).
88
(Nikita Popov)
99
. Add Generator::throw() method. (Nikita Popov)
10+
. Bug #23955: allow specifying Max-Age attribute in setcookie() (narfbg, Lars)
1011

1112
- cURL:
1213
. Added new functions curl_escape, curl_multi_setopt, curl_multi_strerror

ext/session/session.c

+4
Original file line numberDiff line numberDiff line change
@@ -1154,6 +1154,7 @@ static int php_session_cache_limiter(TSRMLS_D) /* {{{ */
11541154

11551155
#define COOKIE_SET_COOKIE "Set-Cookie: "
11561156
#define COOKIE_EXPIRES "; expires="
1157+
#define COOKIE_MAX_AGE "; Max-Age="
11571158
#define COOKIE_PATH "; path="
11581159
#define COOKIE_DOMAIN "; domain="
11591160
#define COOKIE_SECURE "; secure"
@@ -1201,6 +1202,9 @@ static void php_session_send_cookie(TSRMLS_D) /* {{{ */
12011202
smart_str_appends(&ncookie, COOKIE_EXPIRES);
12021203
smart_str_appends(&ncookie, date_fmt);
12031204
efree(date_fmt);
1205+
1206+
smart_str_appends(&ncookie, COOKIE_MAX_AGE);
1207+
smart_str_append_long(&ncookie, PS(cookie_lifetime));
12041208
}
12051209
}
12061210

ext/standard/head.c

+12-7
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ PHP_FUNCTION(header)
4040
{
4141
zend_bool rep = 1;
4242
sapi_header_line ctr = {0};
43-
43+
4444
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|bl", &ctr.line,
4545
&ctr.line_len, &rep, &ctr.response_code) == FAILURE)
4646
return;
47-
47+
4848
sapi_header_op(rep ? SAPI_HEADER_REPLACE:SAPI_HEADER_ADD, &ctr TSRMLS_CC);
4949
}
5050
/* }}} */
@@ -80,7 +80,7 @@ PHPAPI int php_setcookie(char *name, int name_len, char *value, int value_len, t
8080
char *dt;
8181
sapi_header_line ctr = {0};
8282
int result;
83-
83+
8484
if (name && strpbrk(name, "=,; \t\r\n\013\014") != NULL) { /* man isspace for \013 and \014 */
8585
zend_error( E_WARNING, "Cookie names cannot contain any of the following '=,; \\t\\r\\n\\013\\014'" );
8686
return FAILURE;
@@ -111,18 +111,19 @@ PHPAPI int php_setcookie(char *name, int name_len, char *value, int value_len, t
111111
cookie = emalloc(len + 100);
112112

113113
if (value && value_len == 0) {
114-
/*
114+
/*
115115
* MSIE doesn't delete a cookie when you set it to a null value
116116
* so in order to force cookies to be deleted, even on MSIE, we
117117
* pick an expiry date in the past
118118
*/
119119
dt = php_format_date("D, d-M-Y H:i:s T", sizeof("D, d-M-Y H:i:s T")-1, 1, 0 TSRMLS_CC);
120-
snprintf(cookie, len + 100, "Set-Cookie: %s=deleted; expires=%s", name, dt);
120+
snprintf(cookie, len + 100, "Set-Cookie: %s=deleted; expires=%s; Max-Age=0", name, dt);
121121
efree(dt);
122122
} else {
123123
snprintf(cookie, len + 100, "Set-Cookie: %s=%s", name, value ? encoded_value : "");
124124
if (expires > 0) {
125125
const char *p;
126+
char tsdelta[13];
126127
strlcat(cookie, "; expires=", len + 100);
127128
dt = php_format_date("D, d-M-Y H:i:s T", sizeof("D, d-M-Y H:i:s T")-1, expires, 0 TSRMLS_CC);
128129
/* check to make sure that the year does not exceed 4 digits in length */
@@ -136,6 +137,10 @@ PHPAPI int php_setcookie(char *name, int name_len, char *value, int value_len, t
136137
}
137138
strlcat(cookie, dt, len + 100);
138139
efree(dt);
140+
141+
snprintf(tsdelta, sizeof(tsdelta), "%li", (long) difftime(expires, time(NULL)));
142+
strlcat(cookie, "; Max-Age=", len + 100);
143+
strlcat(cookie, tsdelta, len + 100);
139144
}
140145
}
141146

@@ -237,11 +242,11 @@ PHP_FUNCTION(headers_sent)
237242
ZVAL_LONG(arg2, line);
238243
case 1:
239244
zval_dtor(arg1);
240-
if (file) {
245+
if (file) {
241246
ZVAL_STRING(arg1, file, 1);
242247
} else {
243248
ZVAL_STRING(arg1, "", 1);
244-
}
249+
}
245250
break;
246251
}
247252

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

0 commit comments

Comments
 (0)