HOWTO: Persisting Values Without Sessions

ID: Q175167


The information in this article applies to:
  • Microsoft Active Server Pages, versions 1.0, 1.0b
  • Microsoft Visual Studio versions 97, 97sp1, 97sp2, 97sp3
  • Microsoft Internet Information Server versions 4.0, 5.0


SUMMARY

When developing Web sites with HTML forms you may need to keep track of information entered by a user for use at a later time. This process is called persisting values.

Active Server Pages (ASP) provides Session variables to facilitate this; however, these require the use of session IDs. This article describes methods of storing form values without using session cookies.


MORE INFORMATION

Three methods are commonly used to persist values when developing in HTML:

Store the Values in Hidden Form Fields

Storing the values in hidden fields keeps the values out of sight, but intact. A user submits a form, the form values are read and immediately written back out again into an HTML hidden form field. This way, they get carried along with the other fields when that form is submitted.

An example of this can be seen with the following three pages:

   <%@ LANGUAGE="VBSCRIPT" %>
   <!-- This is FORM1.HTM -->

      <HTML>
      <HEAD><TITLE>FORM1.HTM</TITLE></HEAD>
      <BODY>
      <Form Action=Form2.asp Method=Post>
         <Input Type=Text Value="Page1 Value" Name="Value1"><P>

         <Input Type=Submit Name=btnSubmit>
      </Form>
      </BODY>
      </HTML>

      <!-- This is FORM2.ASP -->
        <%@ LANGUAGE="VBSCRIPT" %>
        <%
        Response.Cookies("Value1") = Request("Value1")
        ' If you wish to have the value persisted beyond the current visit,
        ' you must also assign an expiration date as follows:
        Response.Cookies("Value1").Expires = "July 31, 1998"
        %>
        <HTML>
        <HEAD><TITLE>FORM2.ASP</TITLE></HEAD>
        <BODY>
        <Form Action=Form3.asp Method=Post>
          <Input Type=Text Value="Page2 Value" Name="Value2"><P>
          <Input Type=Submit Name=btnSubmit>
        </Form>
        </BODY>
        </HTML>

   <!-- This is FORM3.ASP -->
      <%@ LANGUAGE="VBSCRIPT" %>
      <HTML>
      <HEAD><TITLE>FORM3.ASP</TITLE></HEAD>
      <BODY>
      Value 1 = <%= Request("Value1") %><BR>
      Value 2 = <%= Request("Value2") %><BR>
      </BODY>
      </HTML> 

Append the Values to the URL

This technique is very similar to the first method, but instead of passing the values in hidden variables, the values are appended to the end of the URL and submitted. The disadvantage of this technique is imposed by the size limitations on a URL, which can vary from browser to browser. Additionally, any information, private or otherwise, will be readily visible to the user. The following pages demonstrate this technique:

   <!-- This is FORM1.HTM -->
      <%@ LANGUAGE="VBSCRIPT" %>
      <HTML>
      <HEAD><TITLE>FORM1.HTM</TITLE></HEAD>
      <BODY>
      <Form Action=Form2.asp Method=Post>
         <Input Type=Text Value="Page1 Value" Name="Value1"><P>

         <Input Type=Submit Name=btnSubmit>
      </Form>
      </BODY>
      </HTML>

   <!-- This is FORM2.ASP -->
      <%@ LANGUAGE="VBSCRIPT" %>
      <HTML>
      <HEAD><TITLE>FORM2.ASP</TITLE></HEAD>
      <BODY>

      <Form Action="Form3.asp?value1=<%=
       Server.URLEncode(Request("Value1"))
       %>" Method=Post>
         <Input Type=Text Value="Page2 Value" Name="Value2"><P>

         <Input Type=Submit Name=btnSubmit>
      </Form>
      </BODY>
      </HTML>

   <!-- This is FORM3.ASP -->
      <%@ LANGUAGE="VBSCRIPT" %>
      <HTML>
      <HEAD><TITLE>FORM3.ASP</TITLE></HEAD>
      <BODY>
      Value 1 = <%= Request("Value1") %><BR>
      Value 2 = <%= Request("Value2") %><BR>
      </BODY>
      </HTML> 

Writing Your Own Cookie

This method is least commonly used because it requires that information be written back to the client machine. The only reason to use this method over normal Session based variable is to retain information for a later visit by the browser. This alleviates your need to keep the information on the Web server:

   <!-- This is FORM1.HTM -->
      <%@ LANGUAGE="VBSCRIPT" %>
      <HTML>
      <HEAD><TITLE>FORM1.HTM</TITLE></HEAD>
      <BODY>
      <Form Action=Form2.asp Method=Post>
         <Input Type=Text Value="Page1 Value" Name="Value1"><P>
         <Input Type=Submit Name=btnSubmit>
      </Form>
      </BODY>
      </HTML>

   <!-- This is FORM2.ASP -->
      <%@ LANGUAGE="VBSCRIPT" %>
      <% Response.Cookies("Value1") = Request("Value1") %>
      <HTML>
      <HEAD><TITLE>FORM2.ASP</TITLE></HEAD>
      <BODY>
      <Form Action=Form3.asp Method=Post>
         <Input Type=Text Value="Page2 Value" Name="Value2"><P>
         <Input Type=Submit Name=btnSubmit>
      </Form>
      </BODY>
      </HTML>

   <!-- This is FORM3.ASP -->
      <%@ LANGUAGE="VBSCRIPT" %>
      <HTML>
      <HEAD><TITLE>FORM3.ASP</TITLE></HEAD>
      <BODY>
      Value 1 = <%= Request.Cookies("Value1") %><BR>
      Value 2 = <%= Request("Value2") %><BR>
      </BODY>
      </HTML> 


REFERENCES

For the latest Knowledge Base artices and other support information on Visual InterDev and Active Server Pages, see the following page on the Microsoft Technical Support site:

http://support.microsoft.com/support/vinterdev/

(c) Microsoft Corporation 1997, All Rights Reserved.
Contributions by Paul Enfield, Microsoft Corporation

Additional query words:

Keywords : kbASP kbASPObj kbScript kbVBScript kbVisID100 kbGrpASP kbiis400 kbiis500
Version : :1.0,1.0b; WINDOWS:97,97sp1,97sp2,97sp3; winnt:4.0,5.0
Platform : WINDOWS winnt
Issue type : kbhowto


Last Reviewed: December 20, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.