session| variable One: Global.asa Document of the explanation! Literally! "said the man. Global is the global, the meaning of the world!
We all know! The variable lifetime in the browser is very short! Only dozens of seconds! That is, the download time of a page. So how do we start from life
Track the session-level variables to the end? Rely on ASP file has been powerless! Microsoft told us: rely on our Global.asa bar!
Global.asa is an optional file! He must be placed in your WWW root directory, or under the root directory of the virtual directory. In this way, you can ensure that one of your
Share a Global.asa file with the program. It is he who can begin to trace the Application object to the session object at the beginning of your application.
Second: The composition of Global.asa documents and the use of the explanation!
In order for the server to record the individual user's thread. The Global.asa file must specify
<script Language=scriptlanguage runat=server>
.......
</SCRIPT>
And Runat=server's line of text tells us that he did it on the server. (So you can't use the MsgBox () function, nor can you use the
Document.Write method prompts the user or writes something! After a detailed description)
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>
And the whole process is like this. When you access a site file. If it's HTML, the program won't run! Because there's no application,session in there.
The occurrence of the object. If it is an ASP file, the server first detects the WWW root directory, or there are no Global.asa files in the root directory of the virtual directory. If so, the transport
The corresponding code within the line program! Note, however, that if you do not create an instantiated application,session in your ASP file. The server also does not track
You!
Also: It's worth noting that any object on the server can be used when the OnStart event occurs!
But when the OnEnd event occurs. Be aware that only server,application,session objects are allowed to run in Session_OnEnd. But there are actually more restrictions.
You can't run the Server.mapth method in it. And you can no longer attach values to the session.
Third: How to remind users when the Session object expires---
Finally began to discuss useful topics! This is also a lot of netizens concerned about the problem!
First of all, we discuss how to remind after failure.
Does that remind you? Is it OK to check the session directly ("* *")? (* * For you to detect whether its session ("* *") is invalid value! ) This does not
Well, because the first person coming up may also have their session ("* *") to be empty!
So, let's take a look at where this feature is specifically used. The most obvious example is the shopping page, if you use the session to store the cart. You have to sue.
Tell him if his shopping cart is broken (if the user goes out during the shopping, or if the belly is not feeling well) The value of the session may fail! )
Some thought is a failure to give a hint. But it's hard to achieve! Because the Session_OnEnd event executes on the server side! It can't be passed in time.
You, unless you use the server push method. Actually, it's not good! If you're not there, or you don't have a connection, he can't do it.
So only when the user once again log in to tell you that your last shopping unfinished, only to land again!
This is a good realization. For with a separate customer, if he started shopping then he would trigger the Session_OnStart event to occur. So you're in
Mark the client in the Global.asa! The code is as follows:
<script Language=scriptlanguage runat=server>
.....
Sub Session_OnStart
Response.Cookies ("flag") =1
End Sub
......
</script>
And then write down the detection code in your test code.
Flag=request.cookies ("flag")
If Flag=1 and Session (* *) = "" Then
Response.Write "Your last landing has expired."
Response.End
ElseIf session (* *) = "" Then
Response.Write "You haven't landed yet."
End If
You can write it as a file and then use <--#include ....--> to include it in the file you want to detect, so you can do it!
Why isn't it easier to write code in the Session_OnEnd event?
Unfortunately, I thought of many ways and did not come up with it. Because in the Session_OnEnd event, you can't use Response.write,response.redirect. Right
Like doing something, they are inaccessible. Only a few objects can be used Application,session,server also limited a lot. And you can't give it back in the meantime.
Session (* *) with the value, so he may not be invalidated, hehe!
Four: Before failure to give a hint.
It's a good idea to nip it in the bud, but it's not really useful to get it done. We have discussed it above. This must be a user connected. And it's not like
There is a quick failure of the event, hehe!
So there's really no way out? Actually, we just need to add a few more lines to the above code.
First of all. Or use the Session_OnStart event in the Global.asa file.
Here you want to add.
<script Language=scriptlanguage runat=server>
.....
Sub Session_OnStart
Session.timeout=30 ' here refers to the expiration time of sission, in minutes!
Response.Cookies ("flag") =1
Response.Cookies ("Time") =now () "records his initial session."
End Sub
......
</script>
See here, we should be able to write the detection code without difficulty.
The above code has been written, add a time operation if it is not valid for 5 minutes, prompt him.
should not be difficult!