PHP cookie writes invalid domain

I have a cookie that I use in my application. It looks like this:

+-------+-------+-----------------------+-------+----------+ | Name | Value | Domain | Path | Expires | +-------+-------+-----------------------+-------+----------+ | foo | bar | my.domain.tld | / | Session | +-------+-------+-----------------------+-------+----------+ 

In a section of my script, based on some condition, I am trying to change the value of a cookie. I am using this code:

 // overwrite cookie if($condition){ setcookie("foo", "cat", 0, "/", "my.domain.tld"); } 

Subsequently, my cookie details are as follows:

 +-------+-------+-----------------------+-------+----------+ | Name | Value | Domain | Path | Expires | +-------+-------+-----------------------+-------+----------+ | foo | bar | my.domain.tld | / | Session | | foo | cat | .my.domain.tld | / | Session | +-------+-------+-----------------------+-------+----------+ 

How come that . added to domain? I want to overwrite an existing cookie.

+6
php cookies setcookie
source share
3 answers

As it turned out, it does not indicate that the domain is not working:

 setcookie("foo", "cat", 0, "/"); 

Expected cookie data:

 +-------+-------+-----------------------+-------+----------+ | Name | Value | Domain | Path | Expires | +-------+-------+-----------------------+-------+----------+ | foo | cat | my.domain.tld | / | Session | +-------+-------+-----------------------+-------+----------+ 

Strange, but it works.

0
source share

http://www.php.net/manual/en/function.setcookie.php#93641

The answer is discussed in a post in the php manual.

Cookies are set by the viewing agent and therefore are processed differently depending on the process that the browser uses.

+1
source share

From the documentation:

The domain available to the cookie. To make a cookie accessible in all subdomains of example.com, you must set it to .example.com .. not required, but makes it compatible with a large number of browsers. Setting it to www.example.com will make cookies available only in the www subdomain. For details, see the tail description in the specification.

And tail tail specification is here:

http://curl.haxx.se/rfc/cookie_spec.html

0
source share

All Articles