Вы находитесь на странице: 1из 53

Request object

 When a browser asks for a page from a server, it is called


a request. The Request object is used to get information
from a visitor.
 The querystring collection is used to retrieve the variable
values in the HTTP query string.
 The HTTP query string is specified by the values following
the question mark (?), like this:
<a href= "test.asp?txt=this is a query string test">Link
with a query string</a>
The line above generates a variable named txt with the
value "this is a query string test".
 Query strings are also generated by form submission, or
by a user typing a query into the address bar of the
browser.
Syntax:
Request.QueryString(variable)[(index)|.Count]

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

and names.asp contains the following script:


<%
for i=1 to Request.QueryString("n").Count
  Response.Write(Request.QueryString("n")(i) & "<br />")
next
%>
Output:

The file names.asp would display the


following:

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>

when you click the "Submit" button, those information will be


sent to "this_page.asp" through the url (see page’s url).
Example2: URL

You can just type in the parameter and its value directly in
the address bar. The format is as follow :

page_name.asp ? parameter = value [& parameter = value ...]

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

Information that carries from the client to the


server side.
• Response Header

Information that carries from the server to the


client side.
Server variables
The ServerVariables collection is used to retrieve the
server variable values.

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

INSTANCE_ID The ID for the IIS instance in text format


INSTANCE_MET The meta base path for the instance of IIS that responds to t
A_PATH request
LOCAL_ADDR Returns the server address on which the request came in
LOGON_USER Returns the Windows account that the user is logged into
PATH_INFO Returns extra path information as given by the client
REMOTE_ADDR Returns the IP address of the remote host making the request

REMOTE_HOST Returns the name of the host making the request

REMOTE_USER Returns an unmapped user-name string sent in by the user

REQUEST_MET Returns the method used to make the request


HOD
SCRIPT_NAME Returns a virtual path to the script being executed
SERVER_NAME Returns the server's host name, DNS alias, or IP address as it
would appear in self-referencing URLs
SERVER_PORT Returns the port number to which the request was sent
SERVER_PORT_ Returns a string that contains 0 or 1. If the request is being
SECURE handled on the secure port, it will be 1. Otherwise, it will be 0
SERVER_PROTO Returns the name and revision of the request information
COL protocol
SERVER_SOFTW Returns the name and version of the server software that
ARE answers the request and runs the gateway
URL Returns the base portion of the URL
EXAMPLE:

<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

"firstname" and assign the value "Alex" to it:

<%
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>

And retrieve the values in the "welcome.asp" file like this:

2.) Use a form


<form method="post" action="welcome.asp">
First Name: <input type="text" name="fname" value="" />
Last Name: <input type="text" name="lname" value="" />
<input type="submit" value="Submit" />
</form>

Retrieve the values in the "welcome.asp" file like this:

<%
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

shorter or longer than the default, use the


Timeout property.
 The example below sets a timeout interval of 5

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

 A standardized application programmer's


interface (API) for databases.
 It supports Visual Basic, Visual C++, and
SQL for Access, Paradox, Text, Excel and
Fox-pro databases.
Methods of saving data

 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

 Start by going to the Control Panel (Start


-> Settings -> Control Panel->administrative
tools->ODBC",
It's the "ODBC Data Source Administrator" window. Click
the "File DSN" tab at the top. You should
 Click the Add button. You will now see a list of
different ODBC drivers, select "Microsoft Access
Driver (*.mdb)" and click next.
 Now it asks for where to save the dsn file and what
to call it. Click browse, go to c:\ and create a new
directory called dsn.
 Save it as MyTable_dsn.dsn and click next (the path
to the dsn file should now be
c:\dsn\MyTable_dsn.dsn).
 Click finish. It now wants you to select which
database to connect to.
 Click Select and find the mdb file you created with
Access. It should be in the My Documents folder.
Click OK twice.
Connecting to MyDatabase

 To get information stored in a database with ASP you


need to make a connection to the database through
ODBC. You can look at it like phoning in to the
database.
 To "dial up" our Access database with VBScript we
use this piece of code:
 <%
Set MyConn =
Server.CreateObject("ADODB.Connection")
MyConn.Open
"FILEDSN=c:\dsn\MyTable_dsn.dsn"
%>

Вам также может понравиться