如何删除cookie

huangapple go评论89阅读模式
英文:

How to delete cookie

问题

我写了一个设置和删除 cookie 的网络应用程序。为了澄清我的意思,请看以下代码片段。

package main

import (
	"fmt"
	"github.com/gorilla/mux"
	"net/http"
	"time"
)

func rootHandler(rw http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(rw, "Hello Foo")
}

func setCookieHandler(rw http.ResponseWriter, r *http.Request) {
	c := &http.Cookie{
		Name:     "storage",
		Value:    "value",
		Path:     "/",
		MaxAge:   0,
		HttpOnly: true,
	}

	http.SetCookie(rw, c)
}

func deleteCookieHandler(rw http.ResponseWriter, r *http.Request) {

	c, err := r.Cookie("storage")
	if err != nil {
		panic(err.Error())
	}
	c.Name = "Deleted"
	c.Value = "Unuse"
	c.Expires = time.Unix(1414414788, 1414414788000)
}

func readCookieHandler(rw http.ResponseWriter, r *http.Request) {

	c, err := r.Cookie("storage")
	if err != nil {
		panic(err.Error())
	}
	fmt.Println(c.Expires)
}

func evaluateCookieHandler(rw http.ResponseWriter, r *http.Request) {

	c, err := r.Cookie("storage")
	if err != nil {
		panic(err.Error())
	}

	if time.Now().After(c.Expires) {
		fmt.Println("Cookie is expired.")
	}
}

func main() {
	mux := mux.NewRouter()
	mux.HandleFunc("/", rootHandler)
	mux.HandleFunc("/cookie", setCookieHandler)
	mux.HandleFunc("/delete", deleteCookieHandler)
	mux.HandleFunc("/read", readCookieHandler)
	mux.HandleFunc("/eval", evaluateCookieHandler)

	http.ListenAndServe(":3000", mux)
}

如你所见,当我访问 /cookie 地址时,会按预期设置一个 cookie。然后当我调用 /delete 时,应该从 cookie 中更改名称、值和过期时间。过期时间已更改,但名称和值没有更改。

我想要的是,在身份验证系统中,当用户点击注销按钮时,从浏览器中删除 cookie,以实现注销的功能。

我还发现了这个链接并遵循了建议,但结果并不如预期。

英文:

I wrote a web application that set a cookie and delete it. To clarify to scenario what I mean look at the following code snippet.

package main
import (
"fmt"
"github.com/gorilla/mux"
"net/http"
"time"
)
func rootHandler(rw http.ResponseWriter, r *http.Request) {
fmt.Fprintf(rw, "Hello Foo")
}
func setCookieHandler(rw http.ResponseWriter, r *http.Request) {
c := &http.Cookie{
Name:     "storage",
Value:    "value",
Path:     "/",
MaxAge:   0,
HttpOnly: true,
}
http.SetCookie(rw, c)
}
func deleteCookieHandler(rw http.ResponseWriter, r *http.Request) {
c, err := r.Cookie("storage")
if err != nil {
panic(err.Error())
}
c.Name = "Deleted"
c.Value = "Unuse"
c.Expires = time.Unix(1414414788, 1414414788000)
}
func readCookieHandler(rw http.ResponseWriter, r *http.Request) {
c, err := r.Cookie("storage")
if err != nil {
panic(err.Error())
}
fmt.Println(c.Expires)
}
func evaluateCookieHandler(rw http.ResponseWriter, r *http.Request) {
c, err := r.Cookie("storage")
if err != nil {
panic(err.Error())
}
if time.Now().After(c.Expires) {
fmt.Println("Cookie is expired.")
}
}
func main() {
mux := mux.NewRouter()
mux.HandleFunc("/", rootHandler)
mux.HandleFunc("/cookie", setCookieHandler)
mux.HandleFunc("/delete", deleteCookieHandler)
mux.HandleFunc("/read", readCookieHandler)
mux.HandleFunc("/eval", evaluateCookieHandler)
http.ListenAndServe(":3000", mux)
}

As you can see, when I visit /cookie location, it will be set a cookie as expected. Then when I call /delete, it should change the name, value and expired time from cookie. The expired time is changed, but name and value not.

如何删除cookie

What do I want is, to delete the cookie from browser for sign out in a authentication system, when user click sign out button to delete cookie.
I also discover this link and follow the advice, but does not work as expected.

答案1

得分: 33

删除名为"storage"的cookie,发送带有相同cookie名称的set-cookie。

deleteCookieHandler()应该如下所示:

c := &http.Cookie{
    Name:     "storage",
    Value:    "",
    Path:     "/",
    Expires: time.Unix(0, 0),
    HttpOnly: true,
}

http.SetCookie(rw, c)
英文:

To delete a cookie named "storage", sending set-cookie with the same cookie name.

deleteCookieHandler() should be as follows

c := &http.Cookie{
Name:     "storage",
Value:    "",
Path:     "/",
Expires: time.Unix(0, 0),
HttpOnly: true,
}
http.SetCookie(rw, c)

答案2

得分: 17

MaxAge=0 表示没有指定 'Max-Age' 属性。

MaxAge<0 表示立即删除 cookie,相当于 'Max-Age: 0'。

MaxAge>0 表示存在 Max-Age 属性,并以秒为单位给出。

c := &http.Cookie{
    Name:     "storage",
    Value:    "",
    Path:     "/",
    MaxAge:   -1,
    HttpOnly: true,
}

http.SetCookie(rw, c)
英文:

MaxAge=0 means no 'Max-Age' attribute specified.

MaxAge<0 means delete cookie now, equivalently 'Max-Age: 0'

MaxAge>0 means Max-Age attribute present and given in seconds

c := &amp;http.Cookie{
Name:     &quot;storage&quot;,
Value:    &quot;&quot;,
Path:     &quot;/&quot;,
MaxAge:   -1,
HttpOnly: true,
}
http.SetCookie(rw, c)

答案3

得分: 12

Cookies是按名称进行键控的,因此当你“更改”名称时,实际上是“创建”了一个不同的已过期的cookie。

保持名称相同,它应该可以工作,但不要忘记花些时间了解一下cookie以及它们的工作原理。

英文:

Cookies are keyed by name, so when you "change" the name, you actually "create" a different cookie, already expired.

Keep the name the same and it should work, but don't forget to take some time one day to read about cookies and how they work.

huangapple
  • 本文由 发表于 2014年12月28日 04:13:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/27671061.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定