Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions ext/standard/basic_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -3740,6 +3740,10 @@ PHP_RINIT_FUNCTION(basic) /* {{{ */
/* Default to global filters only */
FG(stream_filters) = NULL;

/* setcookie */
ALLOC_HASHTABLE(SG(cookies));
zend_hash_init(SG(cookies), 0, NULL, NULL, 0);

return SUCCESS;
}
/* }}} */
Expand Down Expand Up @@ -3796,6 +3800,11 @@ PHP_RSHUTDOWN_FUNCTION(basic) /* {{{ */

BG(page_uid) = -1;
BG(page_gid) = -1;

/* setcookie */
zend_hash_destroy(SG(cookies));
FREE_HASHTABLE(SG(cookies));

return SUCCESS;
}
/* }}} */
Expand Down
8 changes: 8 additions & 0 deletions ext/standard/head.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ PHPAPI int php_setcookie(char *name, int name_len, char *value, int value_len, t
sapi_header_line ctr = {0};
int result;
zend_string *encoded_value = NULL;
zend_string *z_name = zend_string_init(name, name_len, 0);

if (name && strpbrk(name, "=,; \t\r\n\013\014") != NULL) { /* man isspace for \013 and \014 */
zend_error( E_WARNING, "Cookie names cannot contain any of the following '=,; \\t\\r\\n\\013\\014'" );
Expand All @@ -92,6 +93,13 @@ PHPAPI int php_setcookie(char *name, int name_len, char *value, int value_len, t
return FAILURE;
}

if (zend_hash_exists(SG(cookies), z_name) == 1) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "should not be used twice with the same name");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's better also print the duplicated cookie name here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I'll see how I can phrase this.
Le 28 sept. 2014 07:38, "Xinchen Hui" notifications@github.com a écrit :

In ext/standard/head.c:

@@ -92,6 +93,13 @@ PHPAPI int php_setcookie(char *name, int name_len, char *value, int value_len, t
return FAILURE;
}

  • if (zend_hash_exists(SG(cookies), z_name) == 1) {
  •   php_error_docref(NULL TSRMLS_CC, E_WARNING, "should not be used twice with the same name");
    

it's better also print the duplicated cookie name here


Reply to this email directly or view it on GitHub
https://github.com/php/php-src/pull/849/files#r18127724.

}

zend_hash_add_empty_element(SG(cookies), z_name);
zend_string_release(z_name);

len += name_len;
if (value && url_encode) {
encoded_value = php_url_encode(value, value_len);
Expand Down
48 changes: 48 additions & 0 deletions ext/standard/tests/network/bug67736-display-errors-off.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
--TEST--
setcookie() emits 2 cookies with same name with display_error off
--DESCRIPTION--
--INI--
display_errors=0
--FILE--
<?php
setcookie('name', 'value');
setcookie('name', 'value');

$expected = array(
'Set-Cookie: name=value',
'Set-Cookie: name=value',
);

$headers = headers_list();

// Filter to get only the Set-Cookie headers
$cookie_headers = [];
foreach ($headers as $header) {
if (strpos($header, 'Set-Cookie:') === 0) $cookie_headers[] = $header;
}

if (count($cookie_headers) !== count($expected)) {
echo "Less headers are being sent than expected - aborting";
return;
}

$bad = 0;

foreach ($cookie_headers as $i => $header) {
if ($header !== $expected[$i]) {
$bad++;
echo "Header mismatch:\n\tExpected: "
. $expected[$i]
. "\n\tReceived: "
. $header
. "\n";
}
}

echo ($bad === 0)
? 'OK'
: 'A total of ' . $bad . ' errors found.';
--EXPECTHEADERS--

--EXPECT--
OK
33 changes: 33 additions & 0 deletions ext/standard/tests/network/bug67736-display-errors-on.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
--TEST--
setcookie() emits 1 cookie then adds a warning for 2nd with same name
--DESCRIPTION--
--INI--
--FILE--
<?php

setcookie('name', 'value');
setcookie('name', 'value');

$expected = array(
'Set-Cookie: name=value',
);

$headers = headers_list();

// Filter to get only the Set-Cookie headers
$cookie_headers = [];
foreach ($headers as $header) {
if (strpos($header, 'Set-Cookie:') === 0) $cookie_headers[] = $header;
}

if (count($cookie_headers) !== count($expected)) {
echo "Less headers are being sent than expected - aborting";
return;
}
--EXPECTHEADERS--

--EXPECTF--

Warning: setcookie(): should not be used twice with the same name in %s

Warning: Cannot modify header information - headers already sent by %s
44 changes: 22 additions & 22 deletions ext/standard/tests/network/setcookie.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,31 @@ setcookie() tests
date.timezone=UTC
--FILE--
<?php
setcookie('name');
setcookie('name', 'value');
setcookie('name', 'space value');
setcookie('name', 'value', 0);
setcookie('name', 'value', $tsp = time() + 5);
setcookie('name', 'value', $tsn = time() - 6);
setcookie('name', 'value', $tsc = time());
setcookie('name', 'value', 0, '/path/');
setcookie('name', 'value', 0, '', 'domain.tld');
setcookie('name', 'value', 0, '', '', TRUE);
setcookie('name', 'value', 0, '', '', FALSE, TRUE);
setcookie('name0');
setcookie('name1', 'value');
setcookie('name2', 'space value');
setcookie('name3', 'value', 0);
setcookie('name4', 'value', $tsp = time() + 5);
setcookie('name5', 'value', $tsn = time() - 6);
setcookie('name6', 'value', $tsc = time());
setcookie('name7', 'value', 0, '/path/');
setcookie('name8', 'value', 0, '', 'domain.tld');
setcookie('name9', 'value', 0, '', '', TRUE);
setcookie('name10', 'value', 0, '', '', FALSE, TRUE);


$expected = array(
'Set-Cookie: name=',
'Set-Cookie: name=value',
'Set-Cookie: name=space+value',
'Set-Cookie: name=value',
'Set-Cookie: name=value; expires='.date('D, d-M-Y H:i:s', $tsp).' GMT; Max-Age=5',
'Set-Cookie: name=value; expires='.date('D, d-M-Y H:i:s', $tsn).' GMT; Max-Age=-6',
'Set-Cookie: name=value; expires='.date('D, d-M-Y H:i:s', $tsc).' GMT; Max-Age=0',
'Set-Cookie: name=value; path=/path/',
'Set-Cookie: name=value; domain=domain.tld',
'Set-Cookie: name=value; secure',
'Set-Cookie: name=value; HttpOnly'
'Set-Cookie: name0=',
'Set-Cookie: name1=value',
'Set-Cookie: name2=space+value',
'Set-Cookie: name3=value',
'Set-Cookie: name4=value; expires='.date('D, d-M-Y H:i:s', $tsp).' GMT; Max-Age=5',
'Set-Cookie: name5=value; expires='.date('D, d-M-Y H:i:s', $tsn).' GMT; Max-Age=-6',
'Set-Cookie: name6=value; expires='.date('D, d-M-Y H:i:s', $tsc).' GMT; Max-Age=0',
'Set-Cookie: name7=value; path=/path/',
'Set-Cookie: name8=value; domain=domain.tld',
'Set-Cookie: name9=value; secure',
'Set-Cookie: name10=value; HttpOnly'
);

$headers = headers_list();
Expand Down
1 change: 1 addition & 0 deletions main/SAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ typedef struct _sapi_globals_struct {
zval callback_func;
zend_fcall_info_cache fci_cache;
zend_bool callback_run;
HashTable *cookies;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe a inline struct here is better , HashTable cookies..

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's wrong with HashTable?
Le 28 sept. 2014 07:39, "Xinchen Hui" notifications@github.com a écrit :

In main/SAPI.h:

@@ -137,6 +137,7 @@ typedef struct _sapi_globals_struct {
zval callback_func;
zend_fcall_info_cache fci_cache;
zend_bool callback_run;

  • HashTable *cookies;

maybe a inline struct here is better , HashTable cookies..


Reply to this email directly or view it on GitHub
https://github.com/php/php-src/pull/849/files#r18127729.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean you can use a inline Hashtable here instead of a Hashtable *, that will avoid Hashtable allocating/freeing.

} sapi_globals_struct;


Expand Down