Thursday 15 September 2011

google app engine - Go : How to Set same Cookie on all pages? -


Once you've entered my URL / is changed and set cookies for login / after setting cookies Home page There is a need to redirect other (page / url: / homepage /). //

How to set the same cookie in all pages?

If you manage a store (see the answer to a signal) of inbuilt library cookies, using however

some GAE is also typical setup (from):

the Google app Engine, create a directory tree inside your app

$ CD MyApp
$ mkdir -p github.com/gorilla
$ cd github.com/gorilla
$ git clone git: // GitHub and there stores clone. Com / gorilla / mux.git

The last line of that example is specific to the package. You have to change with:

Git clone Git: //github.com/gorilla/sessions.git

A quick example:

define your cookie store:

  import ( "github.com/gorilla/sessions" "net / http") // authorization key thing authKey = [] byte {0x70, 0x23 , 0xbd, 0xcb, 0x3a, 0xfd, 0x73, 0x06, 0x46, 0x1c, 0x06, 0xcd, 0x81, 0xfd, 0x38, 0xeb, 0xfd, 0xa8, 0xfb, 0xba, 0x90, 0x4f, 0x8e, 0x3e, 0xa9, 0xb5, 0x43 , 0xf6, 0x54, 0x5d, 0xa1, 0xf2,} // Encryption Key bridesmaid encKey = [] byte {0x31, 0x98, 0x3E, 0x1B, 0x00, 0x67, 0x62, 0x86, 0xB1, 0x7B, 0x60, 0x01, 0xAA, 0xA8 Store = sessions.NewCookieStore (authKey, encKey), 0x76, 0x44, 0x00, 0xEB, 0x56, 0x04, 0x26, 0x9B, 0x5A, 0x57, 0x29, 0x72, 0xA1, 0x62, 0x5B, 0x8C, 0xE9, 0xA1 Øro initSession (R * http.Request) * sessions.Session {session, _: = store.Get (R, "my_cookie") if session.IsNew {session.Options.Domain = "example.org" session.Options.MaxAge = 0 Session.Options.HttpOnly = false session.Options.Secure = true} Return Session}   

And then, in your page operators you just load the cookie, you are like and Set any option to save it again:

  function ViewPageHandler (W. http.ResponseWriter, R * http.Request) {session: = initSession (R) session.Values ​​[" Page "] =" view "session.Save (reading, writing) ....   

Hope that helps.

No comments:

Post a Comment