In-depth research on the global. Asa file and methods for prompting session variable failure

Source: Internet
Author: User
In-depth research on the global. Asa file and methods for prompting session variable failure.

Keywords: ASP

I. Doubts about the global. Asa file! Literally! Global is global. It means global!
We all know! The variable retention period in the browser is very short! Only dozens of seconds! That is, when a page is downloaded
. So how can we track session-level variables from the beginning to the end? ASP files are insufficient!
Microsoft told us: rely on our global. Asa!
Global. ASA is an optional file! It must be stored in your www root directory or the root directory of the virtual directory.
. In this way, make sure that one of your applications shares a global. Asa file. It's exactly what he can do in your application
The Application Object and Session object are always tracked in sequence.

Ii. Composition and usage of the global. Asa file!
To allow the server to record the threads of individual users. The global. Asa file must be specified
<Script language = scriptlanguage runat = Server>
.......
</SCRIPT>
Runat = server tells us that it is actually executed on the server. (So you cannot use msgbox ()
The document. Write method cannot be used to prompt the user or write something! Detailed later)
The complete usage is:
<Script language = scriptlanguage runat = Server>

Sub application_onstart
...
End sub

Sub application_onend
...
End sub

Sub session_onstart
...
End sub

Sub session_onend
...
End sub

</SCRIPT>

The whole process is like this. When you access a site file. If it is HTML, the program will not run! Because
It is impossible to have an application or session object. If it is an ASP file, the server first checks the root object of WWW
Or the root directory of the virtual directory contains the global. Asa file. If yes, run the corresponding code in the program! However, please
NOTE: If no instantiated application or session is created in your ASP file. The server is also not tracked
You!
In addition, it is worth noting that when the onstart event occurs, any object on the server can be used!
However, when an onend event occurs. Note that only server, application, and session pairs are allowed in session_onend.
Like running. But it actually limits more. You cannot run the server. mapth method in it. And you cannot
Session.

3. How to remind the user when the session object fails --- after the session object fails
Finally, I started to discuss useful topics! This is also a concern of many netizens!
First, we will discuss how to remind you of failures.
Do you want to remind me? Can I directly check the value of Session? (** Check the session for you
("**") Indicates whether the value is invalid !) This is not good because the session ("**") of the first person may be empty!

Let's take a look at the specific use of this function. The most obvious example is the shopping webpage. If you use
Session to store the shopping cart. You must tell him whether his shopping cart is invalid (if the user goes out during the next shopping, or
What did you do when your stomach is uncomfortable! The session value may be invalid !)
Some ideas are prompted when they fail. Unfortunately, it is very difficult to implement it! Because the session_onend event runs on the server
OK! It is impossible to tell you in time, unless the server push method is used. Actually not good! If you are not there,
Or it cannot be completed without a connection.
So the user will only tell you that your last shopping is incomplete when logging on again, and you will only need to log on again!
Now we can implement it. For a separate customer, if he starts shopping, the session_onstart event will be triggered.

.
In this way, you can mark the client in global. Asa! The Code is as follows:
<Script language = scriptlanguage runat = Server>
.....
Sub session_onstart
Response. Cookies ("flag") = 1
End sub
......
</SCRIPT>
Then write the detection code in your detection code.
Flag = request. Cookies ("flag ")

If flag = 1 and Session (**) = "" then
Response. Write "Your Last login has expired"
Response. End
Elseif SESSION (**) = "" then
Response. Write "You have not logged on"
End if
You can write it as a file and then use <-- # include... --> to include it in the file you want to detect.
Detection can be completed!

Why isn't it easier to write code in the session_onend event?

Unfortunately, I did not come up with many methods. Because in the session_onend event, you cannot use response. Write, response. Redirect. What objects do, they are not accessible. Only a few objects can be used
Application, session, and server are also limited. You cannot add a value to the session (**) during this period.
He may never expire again!

4. Prompt before failure.
Preventing problems before they happen is a good idea, but it is not really useful. We discussed above. This is required
The user is connected. And there seems to be no event that the session will soon expire, huh, huh!
So there is really no way? In fact, we only need to add a few more lines of the above Code.
First. Or use the session_onstart event in the global. Asa file.
Here you need to add.
<Script language = scriptlanguage runat = Server>
.....
Sub session_onstart
Session. Timeout = 30' here refers to the sission expiration time, in minutes!
Response. Cookies ("flag") = 1
Response. Cookies ("time") = now () 'record the time of his first session creation.
End sub

......
</SCRIPT>

Here, we can easily write the detection code.
Rewrite the code above, and add a time operation. If it fails for another 5 minutes, it will prompt him.
It should be difficult!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.