Published:
17 Oct 2003
|
Abstract
An article that briefly touches over what Global.asa is, how to use it and some examples of what it can do for your website. |
|
by . .
Feedback
|
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days):
27170/
37
|
|
|
What is it? |
Using Global.asa
Published 12/12/01
Introduction
When working with ASP pages,
you've probably used the session object to store variables that you want to
use throughout your website. But what if you want to store the same variables
forever, like connection strings or objects that you use over and over? What
if you want to store variables when a session begins so you can store things
like the time that user logged on? You can now do this (well, you always could
actually) with Global.asa. This article will teach you about what Global.asa
is, what to use it for and what not to use it for.
What is it?
Before I tell you, I must get some
terms clear -
Application - An
application (in ASP terms) is basically your asp site. Usually this is a
virtual directory or maybe the whole thing. An application starts when the web
server starts and ends when the web server stops. Which means that whatever
variable you put in to start when the application starts (we'll cover that
soon) won't stop till it stops.
Session - A session is from
the time that a user starts looking at your site to the time he/she stops.
Session is kept track of by a cookie on the user that gets deleted when the
user closes his/her browser. All session data is stored on the server.
Now that we've got that out of the
way -
Global.asa is a file that goes in
the root directory of your site (or virtual directory) and whenever your
application is started or stopped or whenever a user comes onto your site or
leaves your site it gets activated. That is a pretty ugly description of it
but that's it. The main purpose of it is to store information in Application
variables and Session variables when the program is started.
If you didn't use Global.asa and
wanted to store Application variables, then you would need to have created a
file that created them and run that file when the app started and run it again
if it restarted, the same with session variables. You generally don't know
where a user comes into your site and if you want to assign session variables
then you would have to create the same code on every page to check and create
those variables. Global.asa eliminates this. When a user logs onto your site,
wherever he/she is they get the variables automatically, whenever the
application is started the variables are assigned automatically.
|
Creating the file |
Creating Global.asa
To create the Global.asa, just use
notepad to create a file called Global.asa in your virtual directory or root
directory. Then we can get coding.
The Global.asa have 4 main
sections -
-
Application_onStart() - is
executed whenever an application begins.
-
Application_onEnd() - is
executed whenever an application stops.
-
Session_onStart() - executed
whenever a session beings.
-
Session_onEnd() - its amazing
how this one is executed when a session...... ENDS!
Lets actually create a Global.asa
now.
<script
language="VBScript" RUNAT="SERVER">
Sub Application_OnStart()
Set objConn = CreateObject("ADODB.Connection")
Set Application("objConn") = objConn
Set Application("stTime") = CStr(Now)
End Sub
Sub Application_OnEnd()
Set Application("objConn") = Nothing
Set objConn = Nothing
End Sub
Sub Session_OnStart()
Application.Lock
Application("visitors") = Application("visitors") + 1
Session("visitor") = Application("visitors")
Application.Unlock
End Sub
Sub Session_OnEnd()
End Sub
</script> |
This seems pretty straight forward -
- When the Application Starts - An
ADODB.Connection object is created and made an application variable of the
same name. The current time is stored as a string as well, in an application
variable.
- When the Application Ends - The
connection object gets set to nothing.
- When a Session Starts - We increment
the visitor counter by 1 and set that value to the application variable and
the session variable. We use Application.Lock and Unlock to make sure that
no-one else updates the value at the same time (things could get messy).
- When the Session Ends - Nothing
happens.
You can see that this can be used for some
things that you may do already, and things that you might find useful. Things
to keep note of - The <script> tags and the RUNAT="SERVER" property. Also, the
language doesn't have to be VBScript.
|
Other stuff |
Some other Stuff
You can also have objects created in a
different way - with <object> tags:
<object
id="objRS" RUNAT="Server" SCOPE="Session" Progid="ADODB.RecordSet"></object>
<object id="objCnt"
RUNAT="server" Scope="Application" ProgID="MSWC.Counters"></object>
<script language="VBScript" RUNAT="SERVER">
...
</script> |
You can also use the ClassID in the ProgID
property (if known).
Some uses of this include -
- Storing a parsed XML file in
application/session variables for quick access later in the site.
- Having an open Database connection (be
careful with this one).
- Having a recordset open on every session.
- Put/check cookies on the user and validate
them for the rest of the site.
You should be careful of storing big objects in
these variables as they could get very large. eg. If each session got an
ADODB.Connection object to a database and kept that throughout the visit, with
increasing amounts of visitors, this could have big strains on the database
and your server. Its best to store things in smaller parts, eg. Load up an xml
file and filter it for the information you want, you may want to store the
whole object if its not too big, or store it as an application variable.
Its better to store objects as application
variables because there will only be one of them.
|
Summary |
Summary
Ok, I'm aware of the hopeless job that I did of
explaining what Global.asa is, but now that you've got the hang of it - who
cares! When you look back we haven't covered a lot, but you've learnt things
that can save you time and energy as well as server time and energy (a plus
for lazy programmers). Remember that you can have other subs in Global.asa
(you can't call them anywhere but global.asa though) and it can have anything
else you may have in your ASP pages. So anyway -
|
|
|
User Comments
Title:
exceeeeeeeeeeeeelent
Name:
jiji
Date:
2005-03-09 9:32:41 AM
Comment:
this was the best explanation of for some one who is a beginner and was very helpfull thank you for saving our neck
|
Title:
Global.asa
Name:
P.Prathiba
Date:
2005-02-19 1:02:16 AM
Comment:
This information is very useful for me.
|
Title:
Well Done
Name:
Rizwan
Date:
2005-01-15 4:40:40 AM
Comment:
Great Help from your site.
Keep it up! This type of help are very useful for newbies and sometimes for lazy professionals. lol... Thanks Mr. Author!
|
Title:
Global
Name:
Raj
Date:
2005-01-12 10:28:21 AM
Comment:
This information is quiet useful.
|
Title:
New User
Name:
Mollie...
Date:
2004-10-27 10:48:09 PM
Comment:
Thanks! Your Article helped me out....
|
Title:
Global ASA
Name:
Tonya
Date:
2004-10-24 11:36:05 AM
Comment:
This information is GREAT. Expecially your picture on Virtural and Non-Vertual directories. I have created a small application, created a Global ASA to set a variable to current date. I then created an asp page which will display text along with the date. When I run the page in the browser, it displays my text but not the date. My question is, do I need to turn something on in order for a Global asa to execute?
Thanks for your help.
|
Title:
hi jaan
Name:
Mehr
Date:
2004-09-12 3:42:13 AM
Comment:
i like it this way...keep going...:p
|
Title:
Global.asa
Name:
Niranjan
Date:
2004-08-30 6:16:07 AM
Comment:
THis is very usefull artickle.I was confused with this becuse i was developing the web site without using this.
|
|
Product Spotlight
|
|