Learnem, Computer education over Internet - Since the year 2000
Home Ebook E-learning Courses Free E-books Login Discussion Boards
ASP - Lesson 6 : Application Variables

Application and Application Variables

In previous lesson we saw how we can save information related to current session in session variables.

'Saving data in session variables
Session("testvar")="mac"
'retrieving data from a session variable
somevar=session("testvar")

Now you must be able to guess some of limitations of session variables. Session variables are valid for individual sessions. So, a variable in one session is unknown in another session. This means that variables and information of different users are isolated from each other. Also when a session ends, all data and variables that belong to it will be erased from server memory.

Sometimes you need to share data between all users of a specific application for example consider a counter.

If we use a session variable for counting visits of a page it will only count visits of an individual person in a single session.

Next time he visits the page counter with will start from 1 again. Even if another person visits the page 2 seconds after first person, he will see it has counted 1. (Why?)

For above reasons, another type of variable is created. Application variables are created when our web application is being run for the first time. They remain in server memory until web server is stopped. (Usually this does not happen for many weeks.)

Application variables are accessible to all users sessions of a specific application. So we will be able to share variables  between all users. This is also a way for transferring data from user session to another.

Applications

As you may guess an application is a group of pages, which reside in a directory (and its sub directories) and does a specific task.

This is not all. For each individual application that needs its own settings and properties, you need to create an application in your web server.

You can create an application in your IIS web server by specifying root directory of application. (This can be a subdirectory of your root web directory) Doing this will create a separate application for this directory.

A separate application has its own application variables and runs in its own memory space. Normally a default application is created for your root directory and all its sub directories when your web site is created. If you need a separate application for a subdirectory, you must use IIS manager to create an application for that directory.

If you have purchased a windows hosting service for a hosting company then you can use your default application. Then you
may ask your hosting company to create an application for your specified sub directory on server.

Creating an application for a sub directory in IIS is easy. Open IIS manager console. View its properties by right clicking the name of directory. Go to "home directory" or "virtual directory" tab sheet and press create button to create the application.
(It's obvious that IIS must be installed)
 

Application Variables

Saving data to application variables is the same as session variables.

'Storing data in application variables
Application("counter")=1;
'retrieving data from application variables
count=Application("counter")

Now we can write a counter program that will work.

Example 6-1:

<html><head><title>Counter Example</title></head>
<body>
Page Text
<br><br>

<%count=Application("counter")
count=count+1
Application("counter")=count
Response.write "This page has been viewed " &
Application("counter") & " times"
%>
</body>
</html>


As application is up (and will be for days or even weeks) program will calculate visits to page in the interval that application is running.

In next lessons we will learn to save counter value to file and load it again each time that server starts and stops. Yet there is a small problem in above example. Assume that the value of counter is 100 now. Now two users start browsing the page exactly in the same time. Both browsers will show 101 on their counter. Server will get counter value and increase the value 100 by 1 and assign 101 to counter variable again.

To avoid these situations we can limit number of users that run critical sections of our asp program to one person at a time.

This is done in below example.

Example 6-2:

<html><head><title>Counter Example</title></head>
<body>
Page Text
<br><br>

<%
Application.lock
count=Application("counter")
count=count+1
Application("counter")=count
Application.Unlock
Response.write "This page has been viewed " &
Application("counter") & " times"
%>
</body>
</html>

Global.asa file

You can create a special file for each application called a "global.asa" file. "global.asa" file must be located inside application directory. Global.asa file can contain 4 sub programs.

<script language=vbscript runat=server>

sub session_OnStart
...
End Sub

sub session_OnEnd
...
End Sub

sub Application_OnStart
...
End Sub

sub Application_OnEnd
...
End Sub

</script>

Session_OnStart subroutine runs when a session starts at server and Session_OnEnd is executed when a session ends. Application_OnStart is executed when a page of an application is requested for the first time since web server start time.

Application_OnEnd is executed when web server shuts down. This is the case when we stop web server program or shut down entire server computer.

In our counter example we will use Application_OnEnd subroutine to save last counter value to a file. Then we will use Application_OnStart subroutine to load previous value of page view counter to asp program counter variable when application starts next time.

We can also use this subroutine to initialize necessary variables at the start of the application.

Counting website visits

We can make a small change in our program to count every person arriving to our site for only once. In fact we will not count every visit of the user to pages. To do this we will need to move counter code to Session_OnStart subroutine in global.asa file.

Example 6-3

Session_OnStart
Application.lock
vis=Application("visits")
vis=vis+1
Application("visits ")=vis
Application.Unlock
End sub

In fact we are counting the number of visits to our website not page views. We will also need the following line in one of our pages to show the number of visits to our website.
 

<html>
.
.
.
We have had <%=Application("visits")%> visits to our website
.
.
.
</html>

Showing number of current online visitors

Using the same technique used in our previous example we can show how many users are currently online.

We need to make changes to our global.asa file again.

Global.asa:

Session_OnStart
Application.lock
onvis=Application("onvisitors")
onvis=onvis+1
Application("onvisitors")=onvis
Application.Unlock
End sub

Session_OnEnd
Application.lock
onvis=Application("onvisitors")
onvis=onvis-1
Application("onvisitors")=onvis
Application.Unlock
End sub

Now you can insert below code in your page to show number of online users.

<html>
.
.
.
<%=Application("onvisitors")%> visitors are online now!
.
.
.
</html>

Above application will consist of two files. Global.asa file must reside in the root of application directory. If it is not possible for you to create a new application you can use your default application. This means that you need to copy global.asa file to the root of your web directory. Then you can show the value of "onvisitors" variable on your web pages.
 

When a visitor starts visiting your website, code inside Session_OnStart is executed and "onvisitors" increases by 1. When a user leaves your page or his session times out the variable is decreased by 1. Therefore our variable will show the number of online visitors.

Exercises

1- Create a html login page with the filename "login.html" login.asp is another file which will process data submitted by login.html

login.asp will store username in a session variable and will contain a main menu.

Menu must include a logout link which points to a logout.asp file. This file will clear the username session variable and will contain a link to login page. Prepare html and asp files for described application.

2- We need to put the name of the last person who has logged to above application on our asp web pages (Everyone must be able to see it).

Use Global.asa file to achieve this purpose.


 

Next Lesson


Links
Discussion Boards
Paid Ebooks
Free Ebooks
Testimonials
Faq
Contact
About us
Course Pages
Web Design
C Programming
Free Tutorials
Web Design
PHP Web Prog.
C Prog.
ASP Web Prog.
Free Ebooks
Web Design Ebook
C Prog. Ebook
Free Support
Support for e-books and free courses are offered through CourseFarm website. In order to use the CourseFarm website, You need to create a free member account on it (see main page of the website).

Web Design in 7 Days Course Page

Programming in C in 7 Days Course Page

PHP Web Programming in 7 Days Course Page

Support on CourseFarm is provided by authors of the e-books themselves. You may also contact them on the website for your possible questions.
Home | Educational E-books | Online Courses | Free Books | Discussion Boards
Testimonial | Contact us | FAQ | About us
Society50 Asian Social Network | Science and Tech Blogs | Asia Weblog | CupidB Free MatchMaker

© 2000-2010 Learnem.com, All Rights Reserved. Last Update April, 2011.