Monday, April 11, 2011

Classic ASP Scripting


How To Write Something On the Screen/Site:
response.write("My Name Is Zeeshan")
Declaring A Variable & using its value:
declaring a vairable & using its value as a text
dim name
name="Zeeshan Ahmed Khan"
response.write("My name is: " & name)
Declaring Array & using its value:
it can store 1 extra value like if it is declared for 5 elements it can store 6
dim array(5),i

array(0) = "A"
array(1) = "B"
array(2) = "C"
array(3) = "D"
array(4) = "E"
array(5) = "F"

in for loop;Next is a must
For i=0 to 5
response.write(array(i) & "<br/>")
Next
Looping through the Header &setting its size on the value of I :
response.write("header starting Tag with smaller the value of I bigger the size of header">Text To show " value of i "<Header Ending tag>")
response.write("<h" & i & ">”Header " & i & "</h" & i & ">")
How to get Time :
dim tim
tim =hour(now())
response.write("<p>" & now())
response.write("Pakistan standard Time </p>")
if tim < 12 then
response.write("Good Morning")
else
response.write("Good Day")
end if
Procedure/Function:
<html>
<head>
<%
sub Mutiply(num1,num2)
response.write(num1*num2)
end sub
%>
</head>
<body>
<p> Result: <% call Mutiply(5,5) %></p>
</body>
</html>
Forms/Actions Using GET:
<form method="get" action="Home.asp">
First Name: <input type="text" name="fname" />
<br />
Last Name: <input type="text" name="lname" />
<br /><br />
<input type="submit" value="Submit" />
</form>

<%
if fname<>"" then
response.write("Hello" & fname & "<br/>")
response.write("Have a nice day")
end if
%>
</form>
Forms/Actions Using Request.QueryString:
<%
response.write(request.querystring("fname"))
response.write(" " & request.querystring("lname"))
%>
<form>
First Name: <input type="text" name="fname" />
<br />
Last Name: <input type="text" name="lname" />
<br /><br />
<input type="submit" value="Submit" />
</form>
<%
If fname<>"" Then
      Response.Write("Hello " & fname & "!<br />")
      Response.Write("How are you today?")
      '<form action="Home.asp"></form>
End If
%>
Difference B/W GET & POST:
·        GET does not keep information sent on site private that is its is displayed URL
http://www.w3schools.com/simpleform.asp?fname=Bill&lname=Gates
·        POST keeps it private & does not show it on URL
http://www.w3schools.com/simpleform.asp
 
Radio Buttons:
<%
dim cars     variable in which the value is stored of radio button.
cars=Request.Form("cars")
%>
<form action="Default.asp" method="post">
<p>Please select your favorite car:</p>
Declaring A RADIO BUTTON,giving the variable in which its value will be stored, then checking its value if value is right then setting its radio button checked & setting its value
<input type="radio" name="cars"
<%if cars="Volvo" then Response.Write("checked")%>
value="Volvo">Volvo</input>
<br />
<input type="radio" name="cars"
<%if cars="Saab" then Response.Write("checked")%>
value="Saab">Saab</input>
<br />
<input type="radio" name="cars"
<%if cars="BMW" then Response.Write("checked")%>
value="BMW">BMW</input>
<br />
<input type="radio" name="cars"
<% if cars="Honda" then response.write("checked")%>
value="Honda">HONDA</input>
<br/>
<br/>
<input type="submit" value="Submit" />
</form>
<%
if cars<>"" then
   Response.Write("<p>Your favorite car is: " & cars & "</p>")
end if
%>
How To Create the numbers of visits of a particular user on web site:
It is declared before html tag
<%
dim numvisits
response.cookies("NumVisits").Expires=date+365
numvisits=request.cookies("NumVisits")

if numvisits="" then
   response.cookies("NumVisits")=1
   response.write("Welcome! This is the first time you are visiting this Web page.")
else
   response.cookies("NumVisits")=numvisits+1
   response.write("You have visited this ")
   response.write("Web page " & numvisits)
   if numvisits=1 then
     response.write " time before!"
   else
     response.write " times before!"
   end if
end if
%>



How to Create SESSION & MANAGE it:
The Session object is used to store information about, or change settings for a user session. Variables stored in the Session object hold information about one single user, and are available to all pages in one application.
When does a Session Start: A new user requests an ASP file, and the Global.asa file includes a Session_OnStart Procedure.
When does a Session End:  A session ends if a user has not requested or refreshed a page in the application for a specified period. By default, this is 20 minutes.

If you want to set a timeout interval that is shorter or longer than the default, you can set the Timeout property.
<%
Session.Timeout=5
%>
To end a session immediately, you may use the Abandon method:
<%
Session.Abandon
%>
Store Session Variables:
<%
Session("username")="Donald Duck"
Session("age")=50
%>
Retrieve Session Variables:
<%Response.Write(Session("username"))%>

Session variables can also be used to create login page, but here it is getting the screen resolution.

<%If Session("screenres")="low" Then%> 
  This is the text version of the page
<%Else%> 
  This is the multimedia version of the page
<%End If%>

Remove Session Variables:

This is to remove all data below age 18, in this way we can do different things.

<%
If Session.Contents("age")<18 then 
  Session.Contents.Remove("sale")
End If 
%>

To remove all session varaiables.

<%
Session.Contents.RemoveAll()
%>
To see content being stored during session (When number of content is known):
<%
Session("username")="Donald Duck"
Session("age")=50
dim i
For Each i in Session.Contents
  Response.Write(i & "<br />")
Next
%>

Output:

username
age
To see content being stored during session (When number of content is un-known):
<%
dim i
dim j
j=Session.Contents.Count
Response.Write("Session variables: " & j)
For i=1 to j
  Response.Write(Session.Contents(i) & "<br />")
Next
%>

Output:

Session variables: 2
Donald Duck
50
To get the values of static session variable:
<%
dim i
For Each i in Session.StaticObjects
  Response.Write(i & "<br />")
Next
%>
ASP Application Object:

·         A group of ASP files that work together to perform some purpose is called an application. The Application object in ASP is used to tie these files together.

·         It is used to carry information/data from a user of multiple page web-site.

·         The Application object should hold information that will be used by many pages in the application (like database connection information). This means that you can access the information from any page. It also means that you can change the information in one place and the changes will automatically be reflected on all pages.

Store Application Variables:

You can create Application variables in "Global.asa" like this:

<script language="vbscript" runat="server">
Sub Application_OnStart
application("vartime")=""
application("users")=1
End Sub
</script>
Retrieve Application Variables:
<%
Response.Write(Application("users"))
%>
To see content being stored during session (When number of content is known):
<%
dim i
For Each i in Application.Contents
  Response.Write(i & "<br />")
Next
%>
To see content being stored during session (When number of content is un-known):
<%
dim i
dim j
j=Application.Contents.Count
For i=1 to j
  Response.Write(Application.Contents(i) & "<br />")
Next
%>
To get the values of static Application variable:
<%
dim i
For Each i in Application.StaticObjects
  Response.Write(i & "<br />")
Next
%>
Lock and Unlock(Freezing a Page):
<%
Application.Lock
  'do some application object operations
Application.Unlock
%>
Including Files:

·         The #include directive is used to create functions, headers, footers, or elements that will be reused on multiple pages.

·         You can insert the content of one ASP file into another ASP file before the server executes it, with the #include directive. The #include directive is used to create functions, headers, footers, or elements that will be reused on multiple pages.

How to Use the #include Directive:

Code using #Include Directives

<html>
<body>
<h3>Words of Wisdom:</h3>
<p><!--#include file="wisdom.inc"--></p>
<h3>The time is:</h3>
<p><!--#include file="time.inc"--></p>
</body>
</html>
wisdom.inc
"One should never increase, beyond what is necessary,
the number of entities required to explain anything."
time.inc
<%
Response.Write(Time)
%>

Code not using #include Directives

<html>
<body>
<h3>Words of Wisdom:</h3>
<p>"One should never increase, beyond what is necessary,
the number of entities required to explain anything."</p>
<h3>The time is:</h3>
<p>11:33:42 AM</p>
</body>
</html>
Syntax for Including Files:
<!--#include virtual="somefilename"-->
or
<!--#include file ="somefilename"-->

Virtual Keyword=To define the path

<!-- #include file ="headers\header.inc" -->
Global.asa file:

·         The Global.asa file is an optional file that can contain declarations of objects, variables, and methods that can be accessed by every page in an ASP application.

·         All valid browser scripts (JavaScript, VBScript, JScript, PerlScript, etc.) can be used within Global.asa.

The Global.asa file can contain only the following:

·         Application events

·         Session events

·         <object> declarations 

·         TypeLibrary declarations

·         the #include directive

Events in Global.asa:
In Global.asa you can tell the application and session objects what to do when the application/session starts and what to do when the application/session ends. The code for this is placed in event handlers. The Global.asa file can contain four types of events:
Application_OnStart - This event occurs when the FIRST user calls the first page from an ASP application. This event occurs after the Web server is restarted or after the Global.asa file is edited. The "Session_OnStart" event occurs immediately after this event.
Session_OnStart - This event occurs EVERY time a NEW user requests his or her first page in the ASP application.
Session_OnEnd - This event occurs EVERY time a user ends a session. A user ends a session after a page has not been requested by the user for a specified time (by default this is 20 minutes).
Application_OnEnd - This event occurs after the LAST user has ended the session. Typically, this event occurs when a Web server stops. This procedure is used to clean up settings after the Application stops, like delete records or write information to text files.
General Structure of Global.asa:
<script language="vbscript" runat="server">
sub Application_OnStart
  'some code
end sub
sub Application_OnEnd
  'some code
end sub
sub Session_OnStart
  'some code
end sub
sub Session_OnEnd
  'some code
end sub
</script>
<object> Declarations:

It is possible to create objects with session or application scope in Global.asa by using the <object> tag.

Syntax :

<object runat="server" scope="scope" id="id"
{progid="progID"|classid="classID"}>
....
</object>

Parameter
Description
scope
Sets the scope of the object (either Session or Application)
id
Specifies a unique id for the object
ProgID
An id associated with a class id. The format for ProgID is [Vendor.]Component[.Version]
Either ProgID or ClassID must be specified.
ClassID
Specifies a unique id for a COM class object.
Either ProgID or ClassID must be specified.

 

Examples:

The first example creates an object of session scope named "MyAd" by using the ProgID parameter:

<object runat="server" scope="session" id="MyAd"
progid="MSWC.AdRotator">
</object>

The second example creates an object of application scope named "MyConnection" by using the ClassID parameter:

<object runat="server" scope="application" id="MyConnection"
classid="Clsid:8AD3067A-B3FC-11CF-A560-00A0C9081C21">
</object>

GLOBAL.ASA

The Global ASA would look like this for example # 1

<object runat="server" scope="session" id="MyAd"
progid="MSWC.AdRotator">
</object>

It can be accessed from any ASP page in the following way;

<%=MyAd.GetAdvertisement("/banners/adrot.txt")%> 

Restrictions:

Restrictions on what you can include in the Global.asa file:

·      You cannot display text that is written in the Global.asa file. This file can't display information.

·      You can only use Server and Application objects in the Application_OnStart and Application_OnEnd subroutines. In the Session_OnEnd subroutine, you can use Server, Application, and Session objects. In the Session_OnStart subroutine you can use any built-in object.

Subroutines:

Global.asa is often used to initialize variables.

<script language="vbscript" runat="server">
sub Session_OnStart
Session("started")=now()
end sub
</script>

Global.asa can also be used to control page access.

<script language="vbscript" runat="server">
sub Session_OnStart
Response.Redirect("newpage.asp")
end sub
</script>

you can include functions in the Global.asa file.

In the example below the Application_OnStart subroutine occurs when the Web server starts. Then the Application_OnStart subroutine calls another subroutine named "getcustomers". The "getcustomers" subroutine opens a database and retrieves a record set from the "customers" table. The record set is assigned to an array, where it can be accessed from any ASP page without querying the database:

<script language="vbscript" runat="server">
sub Application_OnStart
getcustomers
end sub
sub getcustomers 
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open "c:/webdata/northwind.mdb"
set rs=conn.execute("select name from customers")
Application("customers")=rs.GetRows
rs.Close
conn.Close
end sub
</script>

Global.asa Example:

In this example we will create a Global.asa file that counts the number of current visitors.

<script language="vbscript" runat="server">
Sub Application_OnStart
Application("visitors")=0
End Sub
Sub Session_OnStart
Application.Lock
Application("visitors")=Application("visitors")+1
Application.UnLock
End Sub
Sub Session_OnEnd
Application.Lock
Application("visitors")=Application("visitors")-1
Application.UnLock
End Sub
</script>

To display the number of current visitors in an ASP file:

<html>
<head>
</head>
<body>
<p>
There are <%response.write(Application("visitors"))%>
online now!
</p>
</body>
</html>

No comments:

Post a Comment