120 likes | 394 Views
Session Variables. Storing Session Variables on the Server. What Is a Session?. When a browser hits a site, the server begins a new Session. Automatically, a unique Session ID is assigned and the browser owns that session. The session lasts until the browser closes.
E N D
Session Variables Storing Session Variables on the Server
What Is a Session? • When a browser hits a site, the server begins a new Session. • Automatically, a unique Session ID is assigned and the browser owns that session. • The session lasts until the browser closes. • During the life of the session, scripts can store data in it.
What Is a Session Variable? • A session variable is a memory location on the Server that can hold a value for the duration of the session. • In ASP, you set a session variable via the command:<% Session(“varName”) = “value”; %> • Replace varName by the actual name of your variable, and replace value by what you want to store there.
Getting Form Data into Session Variables • You can store in Session variables data obtained from the user via forms. • The data persists in the Session variables until the browser closes. • The syntax is:<% Session(“varName") = Request.Form(“inputName").Item; %> • Replace varName by the name of your variable, and replace inputName by the name of the form field.
Working Through an Example • The best way to understand session variables is to work through an example containing three Web pages that you will create: • First, create a form that prompts the user to enter something. • Second, create an ASP page that receives the entry and puts it into a Session variable. • Third, create an ASP page that displays the Session variable.
Creating the Form(page 1 of 3) • Use Dreamweaver to create a new ASP Javascript page. • Save the page as SessionVariablePrompt.asp. • Use the Forms menu to put a form on the page. • Inside the form, create a text input field and a submit button.
Creating the Form(page 2 of 3) • In front of the text field, prompt the user to type something, such as their name:
Creating the Form(page 3 of 3) • Make the name of the text field be:namefield • Make the form’s action be:SessionVariableSet.asp • Save the page containing the form. • Read on to create the page that will handle the form.
Creating the Handler • Use Dreamweaver to create a new ASP Javascript page. • Save the page as SessionVariableSet.asp. • In code view, make the body of the page read as follows:<%Session(“username")= Request.Form(“namefield").item %>The session variable has been created.To see its value, go to the <a href="SessionVariableDisplay.asp">session variable display page</a>. • Save the page.
Creating the Displayer • Use Dreamweaver to create a new ASP Javascript page. • Save the page as SessionVariableDisplay.asp. • In code view, make the body of the page read as follows:The value of the session variable named "username" is: <%= Session("username") %> • Save the page.
Testing the Example • Publish the following pages to your remote site:SessionVariablePrompt.aspSessionVariableSet.aspSessionVariableDisplay.asp • Browse to SessionVariablePrompt.asp at your remote site. Fill out the form, click Submit, and follow the link to the session variable display page. • If the username fails to display, troubleshoot any problems until you get this to work.