Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can setcookie in PHP result in multiple "Set-Cookie" headers?

I am debugging an issue with a Magento system.

The problem is a duplicated Set-Cookie header, like this:

Set-Cookie: flimmit_session=search-0c30086b812519b006aa27ef4f37f15b-24; path=/; domain=.flimmit.com; httponly
Set-Cookie: flimmit_session=search-0c30086b812519b006aa27ef4f37f15b-24; path=/; domain=.flimmit.com; httponly

The cookie is set using php's setcookie command. My question is whether the incorrect use of this function can result in a duplicate Set-Cookie header, or whether I have to look somewhere else for the error...

like image 226
The Surrican Avatar asked Sep 13 '11 13:09

The Surrican


People also ask

Can you have multiple set-cookie headers?

@RobDolinMS Also, you can't have multiple cookie headers in the request. As per RFC 6265 S5. 4: When the user agent generates an HTTP request, the user agent MUST NOT attach more than one Cookie header field.

What is the role of Setcookie () in PHP?

The setcookie() function defines a cookie to be sent along with the rest of the HTTP headers. A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer. Each time the same computer requests a page with a browser, it will send the cookie too.

At what point in a PHP file should the Setcookie () function be called?

A cookie can be set or modified using the following syntax: setcookie(name, value, expire, path, domain, secure, httponly); Note that: Cookies are part of the HTTP header, so setcookie() must be called before any output is sent to the browser.

Does set-cookie overwrite?

Setting a cookie with the same key will cause it to be overwritten.


1 Answers

Yes, calling setcookie() twice with the same data will result in 2 identical Set-Cookie: headers. I have just tried it, and it does.

It shouldn't cause a problem though, the cookie will always have the value defined by the last setcookie() call...

like image 169
DaveRandom Avatar answered Nov 05 '22 18:11

DaveRandom