Introduction

When designing web applications, web developers sometimes want access to variable information that isn't passed through a URL, but is available in the application. For example, a calendar web application could ask visitors for their time zone preference. Then, it could display date and time in that time zone throughout the pages of the application. Session variables can provide this functionality.

Although session variables are a powerful tool in the web developer's arsenal, Dreamweaver does not automatically generate all the code necessary for them to work. This document describes session variables and provides steps for using them in the different server models supported by Dreamweaver.

Prerequisites

Using and understanding this document requires that the developer is working with a server model, such as ASP or ColdFusion. The developer requires working knowledge of the following concepts. Also, they must use a version of Dreamweaver that supports dynamic development (Dreamweaver MX or Dreamweaver MX 2004). See the related sections of the Using Dreamweaver manual or Help system to learn more.

  • Preparing to Build Dynamic Sites
  • Making Pages Dynamic

How session variables work

Session variables allow developers to store visitor information by setting a variable that is accessible to the application for the duration of a visitor's session.

Session variables have the following traits:

  • Unless specified otherwise, session variables expire 20 minutes after a visitor leaves the site.
  • Session variables expire if no activity is detected on the site for 20 minutes by that specific site visitor. They also expire if the visitor quits out of their web browser.
  • In order for session variables to work, it's necessary that the visitor's browser is set to accept cookies.
  • It's necessary that all the pages for the site are located within a single directory on the web server.
  • Information stored in session variables is site visitor specific. Different site visitors cannot access each other's session variable information.

Typically, developers capture the data for the session variables by having a visitor fill out a form in the web application. The session variable is set to the value of a form field. The examples in this TechNote use information from a form element to set the value of a session variable.

For all server models except ASP.NET, Dreamweaver has a menu-driven feature to write session variables to a page after a session variable is created. However, Dreamweaver doesn't provide a menu-driven feature to assign a value to a session variable (also known as instantiating a session variable). It's necessary to assign a value to a session variable manually within the source code.

Create a session variable in ASP, ColdFusion, JSP, or PHP applications

Note: The example for ASP.NET differs substantially from the other server models, since ASP.NET usually uses web forms instead of traditional HTML forms. If you're using the ASP.NET server model, see ASP.NET example at the end of this document.

  1. Define an ASP, ColdFusion, JSP or PHP website.
  2. Create three dynamic pages within the site. The first page contains an HTML form (described in the next step) that submits to the second page. On the second page, create a hyperlink pointing to the third page.

    Note: The reason this example uses three pages is to prove that a session variable is available to a second, third, and subsequent page. Request variables, which are an alternative to session variables, can be carried over from one page to a second. However, they aren't available to a third or subsequent page.

  3. On the first page, create a form that contains one text field form element and a submit button. Make sure that the form control attributes are set as follows:
    • Select the text field and name it "txtFirstName" using the Property inspector.
    • Select the form tag (click the red dashed line surrounding the form controls or click the form tag in the tag selector). Set the second page as the form action in the Property inspector. Specify post as the form method. The "get" method is also frequently used with session variables, but this example uses the post method for simplicity's sake.
  4. On the second page, launch Code view (View> Code). Above the opening <html> tag, type the code for your server model from the list below:

    This code pulls the contents from the form element named"txtFirstName" that was created on the first page. It then creates a session variable named "sessFirstName" which is set equal to the contents of the form element. In other words, this code creates a session variable that holds the information the user enters on the form page.

    • ASP (VBScript) (make sure that the code is contained on a single line, with no line breaks)

      <%session("sessFirstName")=Request.Form("txtFirstName")%>
    • ASP (JavaScript) (make sure that the code is contained on a single line, with no line breaks)
      <%Session("sessFirstName")=String(Request.Form("txtFirstName"))%>
    • ColdFusion
      <cfset Session.sessFirstName = #Form.txtFirstName#>

      To use session variables, ColdFusion also requires that an Application.cfm file is created in the site root, if your site doesn't already have one. The Application.cfm file must contain the following code:

      <CFAPPLICATION NAME="Name" SESSIONMANAGEMENT="Yes">

      Note: On UNIX systems, filenames are case sensitive. Make sure that the Application.cfm filename is spelled with a capital A. See the ColdFusion documentation for other Application.cfm settings.
    • JSP (make sure that the code is contained on a single line, with no line breaks)
      <%session.setAttribute("sessFirstName", request.getParameter("txtFirstName"));%>

      The following code also works, but has been deprecated in the latest Java specs:

      <%session.putValue("sessFirstName", request.getParameter("txtFirstName"));%>
    • PHP
      <?php session_start(); session_register("sessFirstName"); $sessFirstName = $HTTP_POST_VARS['txtFirstName']; ?>

      If a PHP error occurs in the browser on Windows, it could be that the installation of PHP does not have a valid save directory for session data. This parameter is called session.save_path in the PHP.ini file as well as the httpd.conf file. The directory location is optional, but the following setting would work:

      session.save_path = c:\php\sessiondata

      Depending on your PHP configuration, it's sometimes necessary to manually enable session management on all pages related to the session. Place the following line of code at the top of each page:

      <?php session_start();?>

      Instead of calling the session_start() function on each page, you can have PHP start sessions automatically by turning on the session.auto_start option in the php.ini file. More information about configuring PHP for session handling can be found at Session Handling with PHP 4.
  5. Still on this page, open the Bindings panel by choosing Window> Bindings. From the plus (+) menu, choose Session Variable. In the Session Variable dialog box, enter "sessFirstName" (without the quotes) in the name field and click OK.
  6. Expand the session variable icon that appears in the Bindings panel. Drag the "sessFirstName" icon onto the Document window. The session variable contents are written to this page.
  7. Open the third page and repeat steps 4 and 5. Again, make sure that the second page has a link to the third page.
  8. Save, publish, and test all three pages. If you don't get the expected results, check the capitalization and spelling of the form element, session variable, and request variable. The session icon is now visible in the Bindings panel for each page in this site.

 

Create a session variable in ASP.NET

  1. Define an ASP.NET website.
  2. Create two pages within the site. The first page contains an ASP.NET Web Form that posts back to itself and then redirects to the second page.

    Note: The reason this example uses two pages is to prove that a session variable is available to a second, third, fourth, and subsequent pages. Request variables, which are an alternative to session variables, cannot be carried over from an ASP.NET Web Form on one page to a second page

  3. The first page must contain an ASP.NET Web Form that contains one <asp:textbox> server control and an <asp:button> server control. Set the "id" attribute of the text box control to "txtFirstName". The full code for the first page is provided below. Choose either C# or VB, depending on which language you've chosen for your ASP.NET site:

    This code pulls the contents from the <asp:textbox> server control named "txtFirstName." It creates a session variable named"sessFirstName" which is set equal to the contents of the server control. In other words, this code creates a session variable that holds the information the user enters on the form page.

    • ASP.NET (VB) - Page 1 <%@ Page Language="vb"%><script runat="server"> Sub Button1_Click(ByVal s As Object, ByVal e As EventArgs) Session("sessFirstName") = Request("txtFirstName") Response.Redirect("session2.aspx") End Sub</script><html><head><title>Session Page 1</title></head><body> ASP.NET VB session variable and web form example - page 1 <form id="Form1" method="post" runat="server"><p>first name:<asp:TextBox id="txtFirstName" runat="server" /></p><p><asp:Button id="Button1" runat="server" Text="Submit" onClick="Button1_Click" /></p></form></body></html>
    • ASP.NET (C#) - Page 1 <%@ Page Language="C#" %><script runat="server"> void Button1_Click(object sender, EventArgs e) { Session["sessFirstName"] = Request["txtFirstName"]; Response.Redirect("session2.aspx"); }</script><html><head><title>Session Page 1</title></head><body> ASP.NET C# session variable and web form example - page 1<form id="Form1" method="post" runat="server"><p>first name:<asp:TextBox id="txtFirstName" runat="server" /></p><p><asp:Button id="Button1" runat="server" Text="Submit" onClick="Button1_Click" /></p></form></body></html>
  4. In the second page, access Code view by choosing View > Code, and enter the code below to write the session variable contents to the page. Again, choose either C# or VB, depending on which language you've chosen for your ASP.NET site:
    • ASP.NET (VB) - Page 2 <%@ Page Language="vb" %><html><head><title>Session Page 2</title></head><body><p>ASP.NET VB session variable and web form example - page 2</p><p>first name: <%= session("sessFirstName") %></p></body></html>
    • ASP.NET (C#) - Page 2 <%@ Page Language="C#" %><html><head><title>Session Page 2</title></head><body><p>ASP.NET C# session variable and web form example - page 2</p><p>first name: <%=Session["sessFirstName"]%></p></body></html>
  5. Save, publish, and test both pages. If you don't get the expected results, check the capitalization and spelling of the server controls, session variable, and request variable.

Additional information

For more details on session variables, see the following articles:

 

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License  Twitter™ and Facebook posts are not covered under the terms of Creative Commons.

Legal Notices   |   Online Privacy Policy