Request Object
Request Object
Parameter Description
variable Required. The name of the variable in the HTTP
query string to retrieve
index Optional. Specifies one of multiple values for a
variable. From 1 to
Request.QueryString(variable).Count
Example:
To loop through all the n variable values in a Query String:
The following request is sent:
http://www.w3schools.com/test/names.asp?n=John&n=Susan
John
Susan
REQUEST.QUERYSTRING(“PARAMETER”)
Basically, you would find this quite the same as
request.form("parameter"). The difference is that this can be
obtained not only from FORM with the method of "GET" but
also from direct url typing.
Example1: FORM
<form name="form_name" method="get“action="this_page.asp">
<input type="text" name="customer_name" value="">
<input type="password" name="customer_password" value="">
<input type="submit" name="button" value="Submit">
</form>
You can just type in the parameter and its value directly in
the address bar. The format is as follow :
this_page.asp?
customer_name=Linawaty&customer_password=password
Either way, all you have to do is to catch that variable
as follow :
<%
Dim password
Password = request.form("customer_password")
response.write "Customer Password : " & password
%>
Accessing HTTP headers
An HTTP header is a single piece of information sent either
from the client to the server when requesting a page,or
from the server to the client,when responding to the page
request.
Two types of headers are:
Request header
Syntax:
Request.ServerVariables (server_variable)
Parameter Description
server_variable Required. The name of the server variable to
retrieve
Server Variables
Variable Description
ALL_HTTP Returns all HTTP headers sent by the
client. Always prefixed with HTTP_ and
capitalized
ALL_RAW Returns all headers in raw form
APPL_MD_PATH Returns the meta base path for the
application for the ISAPI DLL
APPL_PHYSICAL_PATH Returns the physical path corresponding
to the meta base path
AUTH_PASSWORD Returns the value entered in the client's
authentication dialog
AUTH_TYPE The authentication method that the
server uses to validate users
AUTH_USER Returns the raw authenticated user name
CERT_COOKIE Returns the unique ID for client certificate as a string
CERT_FLAGS bit0 is set to 1 if the client certificate is present and bit1 is
set to 1 if the Certification authority of the client
certificate is not valid
CERT_ISSUER Returns the issuer field of the client certificate
CERT_KEYSIZE Returns the number of bits in Secure Sockets Layer
connection key size
CERT_SECRETKE Returns the number of bits in server certificate private key
YSIZE
CERT_SERIALNU Returns the serial number field of the client certificate
MBER
CERT_SERVER_I Returns the issuer field of the server certificate
SSUER
CERT_SERVER_S Returns the subject field of the server certificate
UBJECT
CERT_SUBJECT Returns the subject field of the client certificate
HTTP_<HeaderName> Returns the value stored in the header
HeaderName
HTTP_ACCEPT Returns the value of the Accept header
HTTP_ACCEPT_LANGU Returns a string describing the language to use for
AGE displaying content
HTTP_COOKIE Returns the cookie string included with the
request
HTTP_REFERER Returns a string containing the URL of the page
that referred the request to the current page
using an <a> tag. If the page is redirected,
HTTP_REFERER is empty
HTTP_USER_AGENT Returns a string describing the browser that sent
the request
HTTPS Returns ON if the request came in through secure
channel or OFF if the request came in through a
non-secure channel
HTTPS_KEYSIZE Returns the number of bits in Secure Sockets
Layer connection key size
HTTPS_SECRETKE Returns the number of bits in server certificate private
YSIZE key
HTTPS_SERVER_I Returns the issuer field of the server certificate
SSUER
HTTPS_SERVER_S Returns the subject field of the server certificate
UBJECT
<html>
<body>
<p>
<b>You are browsing this site with:</b>
<%Response.Write(Request.ServerVariables("http_user_agent"))%>
</p>
<p>
<b>Your IP address is:</b>
<%Response.Write(Request.ServerVariables("remote_addr"))%>
</p>
<p>
<b>The DNS lookup of the IP address is:</b>
<%Response.Write(Request.ServerVariables("remote_host"))%>
</p>
<p>
<b>The method used to call the page:</b>
<%Response.Write(Request.ServerVariables("request_method"))%>
</p>
<p>
<b>The server's domain name:</b>
<%Response.Write(Request.ServerVariables("server_name"))%>
</p>
<p>
<b>The server's port:</b>
<%Response.Write(Request.ServerVariables("server_port"))%>
</p>
<p>
<b>The server's software:</b>
<%Response.Write(Request.ServerVariables("server_software"))%>
</p>
</body>
</html>
Maintaining persistence
Information on the web
Query string
Cookies
Sessions
Application
Cookies
A cookie is often used to identify a user. A cookie is a
small file that the server embeds on the user's computer.
Each time the same computer requests a page with a
browser, it will send the cookie too. With ASP, you can
both create and retrieve cookie values.
Cookies can be temporary or persistent.
Cookie created by asp to track user sessions is temporary
and persists for the duration of the session only
Configuring the browser for cookies
Control panel
Internet options
How to Create a Cookie?
The "Response.Cookies" command is used to create
cookies.
Note: The Response.Cookies command must appear BEFORE
the <html> tag.
In the example below, we will create a cookie named
<%
Response.Cookies("firstname")="Alex“
%>
It is also possible to assign properties
to a cookie, like setting a date when
the cookie should expire:
<%
Response.Cookies("firstname")="Alex“
Response.Cookies("firstname").Expires=#May
10,2012#
%>
How to Retrieve a Cookie
Value?
The "Request.Cookies" command is used to
retrieve a cookie value.
In the example below, we retrieve the value
of the cookie named "firstname" and display it
on a page:
<%
fname=Request.Cookies("firstname")
response.write("Firstname=" & fname)
%>
A Cookie with Keys
If a cookie contains a collection of multiple
values, we say that the cookie has Keys.
In the example below, we will create a cookie
collection named "user". The "user" cookie has
Keys that contains information about a user:
<%
Response.Cookies("user")("firstname")="John"
Response.Cookies("user")("lastname")="Smith"
Response.Cookies("user")("country")="Norway"
Response.Cookies("user")("age")="25“
%>
We want to read all the cookies
sent to a user.
<%
dim x,y
for each x in Request.Cookies
response.write("<p>")
if Request.Cookies(x).HasKeys then
for each y in Request.Cookies(x)
response.write(x & ":" & y &"=" &Request.Cookies(x)(y))
response.write("<br />")
next
else
Response.Write(x & "=" & Request.Cookies(x) & "<br />")
end if
response.write "</p>"
next
%>
</body>
</html>
What if a Browser Does NOT
Support Cookies?
Two ways of doing this:
1.)Add parameters to a URL
<a href="welcome.asp?fname=John&lname=Smith">Go to Welcome Page</a>
<%
fname=Request.form("fname")
lname=Request.form("lname")
response.write("<p>Hello " & fname & " " & lname & "!</p>")
response.write("<p>Welcome to my Web site!</p>")
%>
Session
A Session object stores information about, or change
settings for a user session.
When you are working with an application on your
computer, you open it, do some changes and then you close
it. This is much like a Session.
The computer knows who you are. It knows when you open
the application and when you close it. However, on the
internet there is one problem: the web server does not
know who you are and what you do, because the HTTP
address doesn't maintain state.
ASP solves this problem by creating a unique cookie for
each user. The cookie is sent to the user's computer and it
contains information that identifies the user. This
interface is called the Session object.
When does a Session Start?
A session starts when:
A new user requests an ASP file, and the
Global.asa file includes a Session_OnStart
procedure
A value is stored in a Session variable
A user requests an ASP file, and the Global.asa
file uses the <object> tag to instantiate an object
with session scope.
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
minutes:
<%
Session.Timeout=5
%>
Using Session Variables
<%
CurrTime=NOW
If CurrTime>=#5:00:00# And CurrTime<=#12:00:00# Then
Session(“greeting”)=“Good Morning”
Else
Session(“greeting”)=“Good Evening!”
End if
Response.write(Session(:Greeting”))’ls>
%>
Session ID
The SessionID property returns a unique id for each
user. The unique id is generated by the server.
Syntax
Session.SessionID
<%
Response.Write(Session.SessionID)
%>
Output:
77276603
Application Variables
A group of ASP files that work together to perform some
purpose is called an application.
The Application object is used to store and access
variables from any page, just like the Session object. The
difference is that ALL users share ONE Application
object (with Sessions there is ONE Session object for
EACH user).
<script language="vbscript" runat="server">
Sub Application_OnStart
application("vartime")=""
application("users")=1
End Sub
</script>
You can access the value of an Application
variable like this:
There are
<%
Response.Write(Application("users"))
%>
active connections.
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.
The Global.asa file can contain only the following:
Application events
Session events
<object> declarations
TypeLibrary declarations
the #include directive
Events in Global.asa
Application_OnStart - Occurs when the FIRST user calls
the first page in 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-session ends 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.
<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>
Note:
Because we cannot use the ASP script
delimiters (<% and %>) to insert scripts in
the Global.asa file, we put subroutines
inside an HTML <script> element.
Using databases
A database that stores data in a structure consisting of
one or more tables of rows and columns, which may be
interconnected.
A row corresponds to a record (tuple);
columns correspond to attributes (fields) in the record.
Typically use Structured Query Language (SQL) for data
definition, data management, and data access and
retrieval.
Flat-file database
A relatively simple database system in which each
database is contained in a single table.
In contrast, relational database systems can use multiple
tables to store information, and each table can have a
different record format
ODBC
Cookies
Session variables
Application variables
Text Files
Databases
Creating a database table
When you start Access, it will ask you what you
want to do. Choose "Blank Database" and click OK.
Now Access asks where you want to save the
database file (the mdb file) and what to call it.
Call it MyDatabase.mdb, and save it in My
Documents folder.
A window will now pop up, click New to make a new
table in your database.
Select "Datasheet View" and click OK. Access will
now display your new table:
First we change the name of the field
called "Field1" to "Name". To do this just
double click where it says Field1, then type
in Name and press the Enter key. Do the
same to "Field2", and call it "Link". When
your done it should look like this:
Now you can type in the name of your
friends and the link to their homepage.
I type in my friends Kenneth and Arnstein
and the links to their homepages. My table
looks now like this:
Now we are going to save our table. Just
go to the File menu, and click save.
Access will now prompt you for a name,
type in Friends and click OK. Click NO to
the next question.
Making an ODBC connection