Skip to content

[HttpFoundation] Fix cookie to string conversion for raw cookies #20910

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
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
12 changes: 6 additions & 6 deletions src/Symfony/Component/HttpFoundation/Cookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,20 +80,20 @@ public function __construct($name, $value = null, $expire = 0, $path = '/', $dom
*/
public function __toString()
{
$str = urlencode($this->getName()).'=';
$str = ($this->isRaw() ? $this->getName() : urlencode($this->getName())).'=';

if ('' === (string) $this->getValue()) {
$str .= 'deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001);
} else {
$str .= urlencode($this->getValue());
$str .= $this->isRaw() ? $this->getValue() : urlencode($this->getValue());

if ($this->getExpiresTime() !== 0) {
$str .= '; expires='.gmdate('D, d-M-Y H:i:s T', $this->getExpiresTime());
}
}

if ($this->path) {
$str .= '; path='.$this->path;
if ($this->getPath()) {
$str .= '; path='.$this->getPath();
}

if ($this->getDomain()) {
Expand Down Expand Up @@ -124,7 +124,7 @@ public function getName()
/**
* Gets the value of the cookie.
*
* @return string
* @return string|null
*/
public function getValue()
{
Expand All @@ -134,7 +134,7 @@ public function getValue()
/**
* Gets the domain that the cookie is available to.
*
* @return string
* @return string|null
*/
public function getDomain()
{
Expand Down
6 changes: 4 additions & 2 deletions src/Symfony/Component/HttpFoundation/Tests/CookieTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,12 @@ public function testToString()

public function testRawCookie()
{
$cookie = new Cookie('foo', 'bar', 3600, '/', '.myfoodomain.com', false, true);
$cookie = new Cookie('foo', 'b a r', 0, '/', null, false, false);
$this->assertFalse($cookie->isRaw());
$this->assertEquals('foo=b+a+r; path=/', (string) $cookie);

$cookie = new Cookie('foo', 'bar', 3600, '/', '.myfoodomain.com', false, true, true);
$cookie = new Cookie('foo', 'b+a+r', 0, '/', null, false, false, true);
$this->assertTrue($cookie->isRaw());
$this->assertEquals('foo=b+a+r; path=/', (string) $cookie);
}
}