Golang : Drop cookie to visitor's browser and http.SetCookie() example
There are times developers need to drop cookie to website visitors' browser to determine if a particular visitor has been to the website previously. Cookie can be used to customize how your website behave to a first time visitor or repeat visitor. One such example is the login page usage. Cookie will help your website to determine if the visitor is a first time visitor or not.
For this tutorial, we will learn how to use http.SetCookie()
function to drop cookie to website visitor.
Here is an example code how to change the website message depending on the timestamp inside the visitor's cookie.
package main
import (
"net/http"
"strconv"
"time"
)
func home(w http.ResponseWriter, r *http.Request) {
// setup cookie for deployment
// see http://golang.org/pkg/net/http/#Request.Cookie
// we will try to drop the cookie, if there's error
// this means that the same cookie has been dropped
// previously and display different message
c, err := r.Cookie("timevisited") //
expire := time.Now().AddDate(0, 0, 1)
cookieMonster := &http.Cookie{
Name: "timevisited",
Expires: expire,
Value: strconv.FormatInt(time.Now().Unix(), 10),
}
// http://golang.org/pkg/net/http/#SetCookie
// add Set-Cookie header
http.SetCookie(w, cookieMonster)
if err != nil {
w.Write([]byte("Welcome! first time visitor!"))
} else {
lasttime, _ := strconv.ParseInt(c.Value, 10, 0)
html := "Hey! Hello again!, your last visit was at "
html = html + time.Unix(lasttime, 0).Format("15:04:05")
w.Write([]byte(html))
}
}
func main() {
http.HandleFunc("/", home)
http.ListenAndServe(":8080", nil)
}
run the code above and point your browser to http://localhost:8080
and you should see this message for the first time :
Welcome! first time visitor!
refresh the page again, and this time you will see this message :
Hey! Hello again!, your last visit was at 15:16:02
If you are using Google Chrome browser, go to Views->Developer->Developer Tools and under the Resources tab, you should see Cookies->Localhost.
Delete the cookie named timevisited
and refresh the page again. This time you should be able to see the original message again :
Welcome! first time visitor!
Hope this simple tutorial can be useful to you and happy coding!
References :
See also : Golang : Storing cookies in http.CookieJar example
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+5.4k Javascript : How to refresh page with JQuery ?
+5k JavaScript/JQuery : Redirect page examples
+8.5k Yum Error: no such table: packages
+11.3k Golang : Concurrency and goroutine example
+9.1k Golang : Terminate-stay-resident or daemonize your program?
+6.4k Golang : Warp text string by number of characters or runes example
+62.3k Golang : Convert HTTP Response body to string
+5.8k Golang : Missing Subversion command
+30.6k Golang : Calculate percentage change of two values
+11.2k Golang : Format numbers to nearest thousands such as kilos millions billions and trillions
+29k Golang : JQuery AJAX post data to server and send data back to client example
+18.2k Golang : Write file with io.WriteString