In actual project development, we will often use the Session function in business scenarios. The IRIS framework also provides an easy-to-use, full-featured Session module. The source directory for the Session module is the Kataras/Iris /sessions package.
The difference between Session and Cookie
In learning about Web development, we’ll always be dealing with sessions and cookies. In this lesson we will learn about sesion, so it is necessary to compare and contrast session and cookie:
-
The first is the similarities. Both sessions and cookies are used to store state information about customers. After login and registration, the status information of the account can be stored for subsequent tracking and use.
-
Then there are the differences, and when we talk about the differences between the two let’s conclude from a couple of perspectives. The first is where they are stored. Cookie is stored in the browser of the client for convenient use when the client requests. Session Information is stored on the server and used to store client connection status information.
-
Second, in terms of the types of data that can be stored. Cookie supports only string data type. Session supports multiple data types such as int, String, and bool. Session supports more complete data types.
Session object creation
In actual program development, iris framework can be very convenient to create a new session object. Session creation is as follows:
. sessionID :="mySession"Sess := sessions.New(sessions.Config{Cookie: sessionID,})Copy the code
Supported data types
As mentioned above, sessions support more data types than cookies. Let’s take a look at the data types supported by sessions in the Iris framework:
//String: String session.getString () //Int: unsigned integer and series related units of the same type session.getint () //Boolean: Session.getboolean () //Float: session.getFloat () //interface{} : session.getFlash (); // Interface {} : session.getFlash ()Copy the code
The above code block lists the data types supported for storage in the IRIS framework. We will demonstrate how to use it in this tutorial.
Storage and use of sessions
Session usage is very common in actual project development. In this class, we simulated the storage, use and deletion of Session through a case simulating user login, logout and query.
Specific cases are as follows:
app.Post("/login", func(context context.Context) {
path := context.Path()
app.Logger().Info("Request Path:", path)
userName := context.PostValue("name")
passwd := context.PostValue("pwd")
if userName == "davie" && passwd == "pwd123"Session := sess.start (context) // USERNAME session.set (USERNAME, USERNAME) // login status session.set (ISLOGIN,true)
context.WriteString("Account login successful")}else {
session := sess.Start(context)
session.Set(ISLOGIN, false)
context.WriteString("Account login failed. Please try again.")}})... app.Get("/logout", func(context context.Context) {
path := context.Path()
app.Logger().Info(Exit logon Path:Session. Delete(ISLOGIN) session.Delete(USERNAME) context.writeString ()"Logged out successfully")})... app.Get("/query", func(context context.Context) {
path := context.Path()
app.Logger().Info("Query information path :", path)
session := sess.Start(context)
isLogin, err := session.GetBoolean(ISLOGIN)
iferr ! = nil { context.WriteString("Account not logged in, please log in first.")
return
}
if isLogin {
app.Logger().Info("Account logged in")
context.WriteString("Account logged in")}else {
app.Logger().Info("Account not logged in")
context.WriteString("Account not logged in")}})...Copy the code
The above code shows how to use session, and more information about all the case codes can be found in the accompanying course materials.
Session is used in conjunction with the database
In addition to the use of sessions in programs, sessions can also be bound to a database to synchronize user status sessions to the database and recover them when services are stopped.
db, err := boltdb.New("sessions.db", 0600).iferr ! = nil {panic(err.error ())} // Close the database when the program interruptsfunc() {defer db.close ()}) // Sess.usedatabase (db)Copy the code
In this lesson, we learned about the operation and use of sessions. In the later actual project development, the content we learned in this lesson will be applied to practice.