During web development, the same user can log on only once in some systems at the same time. If a user has logged on, an error must be reported if the user logs on again before exiting. What are the solutions for this situation? When a user logs on, the user first checks whether the user already exists in the application. If the user exists, an error is reported. If the user does not exist, the user is added to the application (the application is shared by all sessions, the unique object of the entire web application ):
The following is a reference clip:
Code:
- Stringstruserid = txtuser. text;
- Arraylistlist = application. Get
- ("Global_user_list") asarraylist;
- If (list = NULL)
- {
- List = newarraylist ();
- }
- For (INTI = 0; I <list. Count; I ++)
- {
- If (struserid = (list [I] asstring ))
- {
- // The error message is displayed when you have logged on.
- Lblerror. Text = "this user has logged on ";
- Return;
- }}
- List. Add (struserid );
- Application. Add ("global_user_list", list );
Of course, you can use cache and other storage methods here.
The next step is to remove the user from the application when the user exits. We can handle it in the session_end event of Global. asax:
Code:
- Voidsession_end (objectsender, eventargse)
- {
- // The code that runs when the session ends.
- // Note: Only the sessionstate mode in the web. config file is set
- // The session_end event is triggered only when inproc is used.
- If the session mode is set to StateServer
- // Or sqlserver, the event is not triggered.
- Stringstruserid = session ["session_user"] asstring;
- Arraylistlist = application. Get ("global_user_list") asarraylist;
- If (struserid! = NULL & list! = NULL)
- {
- List. Remove (struserid );
- Application. Add ("global_user_list", list );
- }
- }
There is no problem with this. The problem is that when you click the close button in the upper-right corner of the browser, the problem occurs. If the session is closed directly, the session expiration event will not be triggered immediately, that is, the session will not be logged in after the browser is closed.
There are two processing methods:
1. Use Javascript
Add a piece of JavaScript code to each page: The following is a reference snippet:
Code:
- Functionwindow. onbeforeunload ()
- {
- If (event. clientx> document. Body
- . Clientwidth & event. clienty <0 | event. altkey ){
- Window. Open ("logout. aspx ");
- }
- }
Because the onbeforeunload method is executed when the browser is closed, refreshed, and the page is adjusted, You need to determine whether you have clicked the close button or pressed Alt + F4 to perform the true close operation.
Write the same method as session_end in page_load of logout. aspx, and add the event onload = "javascript: window. Close ()" to logout. aspx ()"
But there is still a problem. Javascript may have different behaviors in different browsers, and it is not determined when it is closed through the file->.
2. Use the XMLHTTP method (this method is not a problem after testing)
Add the following JavaScript to each page (these javascript can also be written in common, and each page can be introduced). The following is a reference snippet:
Code:
- Varx = 0;
- Functionmyrefresh ()
- {
- Varhttprequest =
- Newactivexobject ("Microsoft. XMLHTTP ");
- Httprequest. Open ("get", "test. aspx", false );
- Httprequest. Send (null );
- X ++;
- If (x <60) // 60 times,
- // That is, the actual expiration time of the session is 30 minutes.
- {
- SetTimeout ("myrefresh ()", 30*1000); // 30 seconds
- }
- }
- Myrefresh ();
- Set in Web. config
- The following is a reference clip:
- <Sessionstatemode =
- "Inproc" timeout = "1"> </sessionstate>
- The test. ASPX page is an empty page,
- You only need to add the following to page_load:
- The following is a reference clip:
- Response. expires =-1;
- Make sure that the cache is not used. You can call this page every time.