The manual Page
Version française
     ASP variables  
[ index | glossary | news | downloads | links ]
 
  introduction to ASP
writting in ASP
ASP variables
the ASP 3.0 objects
the object Request
   .TotalBytes
   .ClientCertificate
   .Cookies
   .Form
   .QueryString
   .ServerVariables
   .BinaryRead
the object Response
   .Status
   .Cookies
   .AddHeader
   .AppendToLog
   .BinaryWrite
   .Clear
   .End
   .Flush
   .Redirect
   .Write
 
news
glossary
downloads
 
guest book
credits
contact
 
 
research
 
last update
25/02/2002
Valid HTML 4.0!
Valid CSS!
Hit-Parade
Measure your audience

Scope and lifetime of variables

In ASP, the scope of variables within the code of a page does not depend on the language we use. This scope is based on these two rules:

  1. any variable defined inside a procedure (or function) cannot but be used inside this procedure (or function); this variable is created and destroyed every time the procedure (or function) is executed.
  2. a variable defined outside all procedures and functions can be read and set by any script (even a procedure or a function) in the ASP page in which it has been declared.

Anyway, a variable is destroyed once the script has been executed. The lifetime of an ASP variable is therefore limited to the execution context of the script in which it has been defined.

Exchanging variables

According to the previous paragraph, it seems not to be possible to exchange variables between ASP pages, but actually this problem can be solved.

The GET method

This is this easiest solution. It consists of passing the variable via the URL of the page that must get it. This solution is very convenient for redirects and links:

<%
' We are going to create a variable var1 and pass it to the page2.asp
' script via its URL (page2.asp?var1=foo...)
var1 = "foo"

' For a redirect to page2.asp, we do this
Response.Redirect("page2.asp?var1=" & var1)

' For a link to page2.asp, we do this
Response.Write("<A href=""page2.asp?var1=""" & var1 & ">my link</A>")
%>

In this example, page2.asp gets the content of var1 via the URL. It is then possible to use this variable as follows:

<%
...
var1 = Request.QueryString("var1")
...
%>

Request.QueryString is a collection that represents the query string of the GET request that called the ASP page.

The drawback of this method is that we have to add systematically the string "?var1=..." to all links.

The Application object

We should use his method only when we want to define variables available to all ASP scripts. Application variables are always available, no matter the page or the visitor. We can consider application variables are global variables. An example of this was given in the previous chapter.

Using this object implies some constraints (see the chapter dedicated to this object).

Sessions

This is maybe the best method. Sessions are used to store user-specific variables. Session variables are available to all pages of an ASP application. For instance, we use session variables to store logins and passwords for users:

<%
...
' If the login and password are valid, we store them and the
' start date in session variables
Session("start") = now()
Session("login") = login
Session("password") = password
...

' To use these variables, we do this
Response.Write("Your login is: " & Session("login"))
%>

We specify a timeout for sessions: when the session is over, all session variables are destroyed.

The use of sessions if often related to the use of cookies: it may not be possible to use sessions with non cookie-compliant Web browsers.

References

The W3Schools Web site

printable format printable format



Copyright © 2000-2001 themanualpage.org - This site is submissive to the terms of the GNU GPL and FDL licences.