How to Store ASP Variables in Memory

How to Store ASP Variables in Memory thumbnail
Store ASP variables in memory to maintain them between server requests.

HTTP, the protocol that allows browsers to communicate with Web servers, is stateless. Whenever a browser requests information from a server, the server sees the request as a new event. The server has no memory of the browser or any previous interaction with it. ASP, an older programming language, solves this problem by allowing developers to create "Session" objects. Using an ASP Session object, you can store a site visitor's data in session memory variables and retrieve them as needed.

Instructions

    • 1

      Open Notepad and add the following text to a new document:

      <html>

      <body>

      <%

      %>

      </body>

      </html>

      This creates an empty ASP file.

    • 2

      Add the following code after the first percent sign:

      response.write("Testing ASP Memory Storage <br/> <br/>")

      response.write("The time is " & Time())

      This code displays the current time.

    • 3

      Add this code below the code in step 2:

      dim lastVisited

      lastVisited = Session("lastVisitedStored")

      response.write("<br/> You last visited at ...." & lastVisited)

      The first line creates a variable named "lastVisted." The next line retrieves the session memory variable named "lastVisitedStored" and saves it in the "lastVisited" variable. The third line displays the value stored in "lastVisited."

    • 4

      Add the following code below the code in step 3:

      Session("lastVisitedStored") = Time()

      This stores the current time in the session variable "lastVisitedStored."

    • 5

      Click "File" and select "Save As." Notepad will display the "Save As" window. Enter a name for the file in the "File Name" text box. Append the extension ".asp" to the end of the file. For example, to name the file "Test1," type "Test1.asp" (without the quotes) in the text box. Click "Save" to save the file.

    • 6

      Upload the file to your Web server. Open a browser and navigate to the file. The first time you visit the page, the ASP code will display the current time and store it in the "lastVisitedStored" session variable.

    • 7

      Press F5 to refresh your browser. The ASP code will retrieve the value stored in the session variable and display it along with the current time. The two values will differ.

    • 8

      Continue to press F5. Each time you refresh the browser, the ASP code will retrieve the previous time from the session variable and display it along with the current time.

Tips & Warnings

  • Create as many session memory variables as you need. A user's session remains active until It times out or the ASP program ends it.

Related Searches:

References

Resources

  • Photo Credit background with binary data image by Pedro Nogueira from Fotolia.com

Comments

You May Also Like

Related Ads

Featured