1 / 12

Session Variables

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.

hashim
Download Presentation

Session Variables

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Session Variables Storing Session Variables on the Server

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. 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.

  7. Creating the Form(page 2 of 3) • In front of the text field, prompt the user to type something, such as their name:

  8. 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.

  9. 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.

  10. 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.

  11. 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.

  12. Sample Test Run

More Related