In-depth study of application and session objects (including Global.asa) 3

Source: Internet
Author: User
Tags count modify root directory
application|session| Objects III, Global.asa

ASP's application and session objects embody features that are not in the other ASP's built-in objects-events. Each visitor accesses the server and triggers a OnStart event (the first visitor triggers both the application and session OnStart events, but application is preceded). A OnEnd event is triggered at the end of each visitor's session (the OnEnd event of the application and session is triggered at the end of the last guest conversation, but preceded by application).

OnStart and OnEnd These two events are commonly used in virtual communities to count online numbers, modify users ' online offline status, and so on. To define both events, you need to write the code in the Global.asa file and place the file in the root directory of the site (by default, \inetpub\wwwroot\). In addition, application and session objects specify other ASP built-in objects (Response, Request, Server, session ...) In addition to application objects in the OnEnd event. ) can not be used. Here is an example of how to use these two events with a virtual Community statistics online number.

File Description:

Global.asa is located in the D:\Inetpub\wwwroot\ directory

Default.asp is located in the D:\Inetpub\wwwroot\ directory, the virtual Community login page

Login.asp is located in the D:\Inetpub\wwwroot\ directory, used to detect user input username and password

Index.asp is located in the D:\Inetpub\wwwroot\ directory, the virtual community home

Bbs.mdb is located in the D:\Inetpub\wwwroot\ directory, the database where user information is stored

Database (ACCESS) Structure:

===bbs table = = =

ID user ID, long integer

Name username, text type

Code password, text type

Online status, yes/No

 
===global.asa===

<script language= "VBScript" runat= "Server"

Sub Application_OnStart

Application ("online") =0

End Sub

Sub Application_OnEnd

nd Sub

Sub Session_OnStart

End Sub

Sub Session_OnEnd

If Session.Contents ("pass") then ' to determine whether to be a logged-on user's Session_OnEnd

Application.Lock

Application ("Online") =application ("online")-1

Application.UnLock

End If

End Sub

</script>

==============

===login.asp===

... ' Password authentication, connect the database, detect the user entered the user name and password is correct

If password verification through then

Session ("name") =rs ("name")

Session ("id") =rs ("id")

Session ("Pass") =true

Else

Rs.close

Conn.close

Response.Write "Bad password! "

Response.End

End If

Application.Lock

Application ("Online") =application ("online") +1

Conn. Execute ("Update BBS set online=1 where id=" &session ("id")) ' sets the user's state to online

Application.UnLock

Rs.close

Conn.close

Response.Redirect "index.asp" ' Initialize data and jump to the home page of the community

===========

In this case, the application ("online") variable is used to record the number of online people who have logged in to the community, because once a user accesses the server regardless of whether the user is logged on, the OnStart event is generated, so Applicaiton (" Online ") plus one. Because whether or not the end of a logged-on user's session generates a OnEnd event (if a visitor accesses the server but does not log in to the community, and a OnEnd event occurs after his session ends), the Session_ The OnEnd event uses a sentence if statement to determine whether the logged in user's OnEnd event, if it is the number of people online minus one.

This is just a simple example of the number of people online, for a complete virtual community, it is not enough to count the number of users online, in this case there is a online field in the database to record the user's status, the user logged in, In the Login.asp will be online set to 1, but the user offline and did not set the online to 0, to improve it, it is necessary to modify the Session_OnEnd event, the event in the online set to 0.

===global. sas===

<script language= "VBScript" runat= "Server"

Sub Application_OnStart

Application ("online") =0

Set Application ("conn") =server.createobject ("ADODB. Connection ")

Application ("DB") =server.mappath ("\bbs.mdb") ' It is best to use absolute path \bbs.mdb here, described in detail below

End Sub

Sub Application_OnEnd

Set Application ("conn") =nothing

End Sub

Sub Session_OnStart

End Sub

Sub Session_OnEnd

If Session.Contents ("pass") then ' to determine whether to be a logged-on user's Session_OnEnd

Application ("Con"). Open = "Driver={microsoft Access driver (*.mdb)};d bq=" &application ("db")

Application.Lock

Application ("Online") =application ("online")-1

Application ("Con"). Execute ("Update friends set online=0 where id=" &session.contents ("id"))

Application.UnLock

Application ("Con"). Close

End If

End Sub

</script>

==============

At this point, the complete code is complete. Because the server object cannot be used in the application and session OnEnd events, the connection to the database and the physical address of the database on the server (d:\inetpub\wwwroot\ Bbs.mdb) is stored in the application variable and is processed in the Application_OnStart event in advance. Similarly, it is not possible to replace session.contents ("pass") with the session ("pass") in the Session_OnEnd event (with a detailed description below).

Four, two points worth noticing in the example of this article

The session.contents in the ⒈onend incident

Friends who have just started to contact Global.asa often take the Session_OnEnd event



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.