Cookies on localhost with explicit domain

前端 未结 21 1780
[愿得一人]
[愿得一人] 2020-11-22 04:17

I must be missing some basic thing about cookies. On localhost, when I set a cookie on server side and specify the domain explicitly as localhost (or .localhost). t

相关标签:
21条回答
  • 2020-11-22 05:06

    Cookie needs to specify SameSite attribute, None value used to be the default, but recent browser versions made Lax the default value to have reasonably robust defense against some classes of cross-site request forgery (CSRF) attacks.

    Along with SameSite=Lax you should also have Domain=localhost, so your cookie will be associated to localhost and kept. It should look something like this:

    document.cookie = `${name}=${value}${expires}; Path=/; Domain=localhost; SameSite=Lax`;
    

    https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie/SameSite

    0 讨论(0)
  • 2020-11-22 05:07

    After much experimentation and reading various posts, this worked. I could set multiple cookies, read them back and set the time negative and delete them.

    func addCookie(w http.ResponseWriter, name string, value string) {
        expire := time.Now().AddDate(0, 0, 1)
        cookie := http.Cookie{
           Name:    name,
           Value:   value,
           Expires: expire,
           Domain:  ".localhost",
           Path:    "/",
        }
        http.SetCookie(w, &cookie)
    }
    
    0 讨论(0)
  • 2020-11-22 05:09

    I broadly agree with @Ralph Buchfelder, but here's some amplification of this, by experiment when trying to replicate a system with several subdomains (such as example.com, fr.example.com, de.example.com) on my local machine (OS X / Apache / Chrome|Firefox).

    I've edited /etc/hosts to point some imaginary subdomains at 127.0.0.1:

    127.0.0.1 localexample.com
    127.0.0.1 fr.localexample.com
    127.0.0.1 de.localexample.com
    

    If I am working on fr.localexample.com and I leave the domain parameter out, the cookie is stored correctly for fr.localexample.com, but is not visible in the other subdomains.

    If I use a domain of ".localexample.com", the cookie is stored correctly for fr.localexample.com, and is visible in other subdomains.

    If I use a domain of "localexample.com", or when I was trying a domain of just "localexample" or "localhost", the cookie was not getting stored.

    If I use a domain of "fr.localexample.com" or ".fr.localexample.com", the cookie is stored correctly for fr.localexample.com and is (correctly) invisible in other subdomains.

    So the requirement that you need at least two dots in the domain appears to be correct, even though I can't see why it should be.

    If anyone wants to try this out, here's some useful code:

    <html>
    <head>
    <title>
    Testing cookies
    </title>
    </head>
    <body>
    <?php
    header('HTTP/1.0 200');
    $domain = 'fr.localexample.com';    // Change this to the domain you want to test.
    if (!empty($_GET['v'])) {
        $val = $_GET['v'];
        print "Setting cookie to $val<br/>";
        setcookie("mycookie", $val, time() + 48 * 3600, '/', $domain);
    }
    print "<pre>";
    print "Cookie:<br/>";
    var_dump($_COOKIE);
    print "Server:<br/>";
    var_dump($_SERVER);
    print "</pre>";
    ?>
    </body>
    </html>
    
    0 讨论(0)
  • 2020-11-22 05:10

    I had the same issue and I fixed it by putting 2 dots in the cookie name itself without specifying any domain.

    set-cookie: name.s1.s2=value; path=/; expires=Sun, 12 Aug 2018 14:28:43 GMT; HttpOnly
    
    0 讨论(0)
  • 2020-11-22 05:14

    Results I had varied by browser.

    Chrome- 127.0.0.1 worked but localhost .localhost and "" did not. Firefox- .localhost worked but localhost, 127.0.0.1, and "" did not.

    Have not tested in Opera, IE, or Safari

    0 讨论(0)
  • 2020-11-22 05:14

    document.cookie = valuename + "=" + value + "; " + expires + ";domain=;path=/";

    this "domain=;path=/"; will take dynamic domain as its cookie will work in subdomain. if u want to test in localhost it will work

    0 讨论(0)
提交回复
热议问题