In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you about how to save user status with Session. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
Online shopping has become an indispensable part of life now. We only go to Taobao for consumption. Today, we look at Taobao from a different point of view.
As shown above:
Case 1: when you open some web pages related to the account and detect that the user is not logged in, the system will automatically jump to the login interface.
Case 2: when it is detected that a user has logged in, the page will automatically jump to the destination page.
Question: how does the system detect whether the user is logged in and how to save the user's login status?
Let's talk about my understanding: using Session to save user state.
Solution 1: each user has a Session object corresponding to it. When the user wants to jump to any interface related to the account (the purchased treasure page), each page should be loaded to determine the user state saved by the Session object.
1. The code of the login interface:
Public partial class Login: System.Web.UI.Page {protected void Page_Load (object sender, EventArgs e) {} protected void Login1_Authenticate (object sender, AuthenticateEventArgs e) {Session ["UserFlag"] = false / / first set the session value UserFlage to indicate whether the user is logged in to if (UserVolidate (Login1.UserName, Login1.Password) = = true) / / call the custom method to verify whether the user has logged in successfully {Session ["UserFlag"] = true; / / e.Authenticated = true; Response.Redirect ("Main.aspx") / / Jump main interface} else {Response.Write ("not logged in") / / otherwise prompt not to log in}} / user name / / user name / / user password / bool private bool UserVolidate (String userName String userPassword) {/ / Connect to the database SqlConnection con = new SqlConnection ("server=. Database=User;uid=sa;pwd=123456; "); con.Open (); SqlCommand cmd=new SqlCommand (" select * from UserInfo where UserName= "+ userName +" and Password= "+ userPassword); int num=cmd.ExecuteNonQuery (); if (num > 0) {return true;} else {return false }
two。 Code for the jump interface: (prevent users from jumping directly to the interface through URL, and the interface has to be judged each time)
Public partial class Main: System.Web.UI.Page {/ / form load protected void Page_Load (object sender, EventArgs e) {/ / determine whether a user logs in to if based on the login status saved by the Session object (Session ["UserFlag"]. ToString () = = "false") {/ / if not logged in Then jump directly to the login interface Response.Redirect ("Login.aspx") }
Solution 2: by providing an intermediary judge.aspx interface, each interface related to the account jumps to the interface before jumping, and the login status of the user is judged when the interface is loaded.
1. The login page code remains the same
2. Judge.aspx page code:
Public partial class judge: System.Web.UI.Page {protected void Page_Load (object sender, EventArgs e) {/ / A pair of Session objects to determine the status of the user if (Session ["flag"] .ToString () = = "false") {Response.Write ("not logged in") } / * else {Response.Redirect ("Main.aspx"); / / adding this sentence will create an endless cycle} * /}
3. Jump interface code:
Public partial class Main: System.Web.UI.Page {protected void Page_Load (object sender, EventArgs e) {/ / the jump page must be judged to prevent direct login to the page without logging in. Server.Execute ("judge.aspx"); / / by executing the Execute method of the Server object}}
Summary: the principle of the two solutions is the same: the user's state is saved through the Session object, and different pages can judge the user's state by accessing the session value of the user's Session object. The second solution only simplifies the judgment process, first executes the intermediary judgment interface through the Execute method of the Server object, and then returns to the original program execution order.
Add:
Server object
Syntax: Server.Execute (path)
Purpose: this method is a new function of IIS5.0, which is similar to the function call in the program language, that is, you can use the Server.Execute (path) method in the ASP program to call the ASP program specified by Path, and then return to the original program after the execution of the called program, and continue to execute the next instructions.
Session object
In the site, each newly visited user will generate his or her own session object. This session object is managed on the server side and can only be served by the currently accessed user. If another user enters the site, he or she will have his own session object, and the session object of the two users cannot be shared even if they have the same name.
The above is the editor for you to share how to use Session to save user status, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.