Cold Fusion Tips-N-Tricks
Managing Application and Session variables

Ever wanted to muck around with your application and session variables without having to hard code stuff in your programs? Attached are two programs that will allow you to view, edit, delete and/or add session and application variables.

The first program, EditSession.cfm, displays the variables. The 2nd program actually performs the edits and then redirects back to the 1st program to display the new values.

You can do one of two things to your application/session variables: You can delete a variable or you can edit/add a variable. If you wanted to delete a variable to see how your program would react, set the checkbox next the variable in question and click on the button labeled "Delete Selected Variables". If you wanted to edit the values of existing variables, type in the changes you want and click on the "Update Application/Session Variables" button. At the bottom of both the application and session variable listing there are two blank text boxes where you can enter a new variable name and it's value (if any). Clicking the Update button creates the variables.

Note that application/session structures, queries, and arrays will be displayed but you will not be able to edit the contents.

Simply save the code below for the two programs to your project directory and point your browser to SessionEdit.cfm



Example HTML/CFML code:
===================================================================================================
SessionEdit.cfm
===================================================================================================

<cfsetting enablecfoutputonly="Yes" showdebugoutput="No">

<!---

Session/Application variable edit, by John Bartlett (jbartlett@nxs.net)

--->

<!--- Fetch the session and application variables --->
<CFLOCK timeout="2" throwontimeout="No" name="EditApp" type="EXCLUSIVE">
<CFSET sess=ListSort(StructKeyList(session,","),"TextNoCase","ASC",",")>
<CFSET app=ListSort(StructKeyList(application,","),"TextNoCase","ASC",",")>
</CFLOCK>

<CFOUTPUT>
<html>
<head>
<title>Application/Session variable edit</title>
<META HTTP-EQUIV="expires" CONTENT="Fri, 1 Jan 1990 00:00:00 GMT">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="cache-control" VALUE="no-cache, no-store, must-revalidate">
</head>
<body bgcolor="##FFFFFF" text="##000000">
<form method="POST" action="SessionEditUpdate.cfm">
<font face="Arial" size="4"><b>Application Variables</b></font>
<table border="1" cellpadding="3" cellspacing="0">
</CFOUTPUT>

<CFSET EditApp="">
<CFSET EditSess="">

<!--- Display all application variables --->
<CFLOOP index="var" list="#app#">
  <CFLOCK timeout="2" throwontimeout="No" name="EditApp" type="EXCLUSIVE">
  <CFSET tmp=Evaluate("application.#var#")>
  </CFLOCK>
  <CFSET out=tmp>
  <CFSET edit=1>
  <CFIF IsArray(tmp)>
    <CFSET out="[Array]">
    <CFSET edit=0>
  <CFELSEIF IsQuery(tmp)>
    <CFSET out="[Query]">
    <CFSET edit=0>
  <CFELSEIF IsStruct(tmp)>
    <CFSET out="[Structure]">
    <CFSET edit=0>
  </CFIF>
  <CFOUTPUT>
  <tr>
    <td><input type="Checkbox" name="app" value="#var#"></td>
    <td>#LCase(var)#</td>
    <CFIF edit EQ 1>
      <CFSET EditApp=ListAppend(EditApp,var)>
      <td>
        <input type="Text" name="i_app_#var#" value="#out#" size="50">
        <input type="Hidden" name="o_app_#var#" value="#out#">
      </td>
    <CFELSE>
      <td>#out#</td>
    </CFIF>
  </tr>
  </CFOUTPUT>
</CFLOOP>

<CFOUTPUT>
  <tr>
    <td>&nbsp;</td>
    <td><input type="Text" name="NewApp" size="15"></td>
    <td><input type="Text" name="NewAppVal" size="50"></td>
  </tr>
</table>
<br><br>
<font face="Arial" size="4"><b>Session Variables</b></font>
<table border="1" cellpadding="3" cellspacing="0">
</CFOUTPUT>

<!--- Display all session variables --->
<CFLOOP index="var" list="#sess#">
  <CFLOCK timeout="2" throwontimeout="No" name="EditApp" type="EXCLUSIVE">
  <CFSET tmp=Evaluate("session.#var#")>
  </CFLOCK>
  <CFSET out=tmp>
  <CFSET edit=1>
  <CFIF IsArray(tmp)>
    <CFSET out="[Array]">
    <CFSET edit=0>
  <CFELSEIF IsQuery(tmp)>
    <CFSET out="[Query]">
    <CFSET edit=0>
  <CFELSEIF IsStruct(tmp)>
    <CFSET out="[Structure]">
    <CFSET edit=0>
  </CFIF>
  <CFOUTPUT>
  <tr>
    <td><input type="Checkbox" name="sess" value="#var#"></td>
    <td>#LCase(var)#</td>
    <CFIF edit EQ 1>
      <CFSET EditSess=ListAppend(EditSess,var)>
      <td>
        <input type="Text" name="i_sess_#var#" value="#out#" size="50">
        <input type="Hidden" name="o_sess_#var#" value="#out#">
      </td>
    <CFELSE>
      <td>#out#</td>
    </CFIF>
  </tr>
  </CFOUTPUT>
</CFLOOP>

<CFOUTPUT>
  <tr>
    <td>&nbsp;</td>
    <td><input type="Text" name="NewSess" size="15"></td>
    <td><input type="Text" name="NewSessVal" size="50"></td>
  </tr>
</table>
<br>
<input type="Hidden" name="EditApp" value="#EditApp#">
<input type="Hidden" name="EditSess" value="#EditSess#">
<input type="Submit" name="submit" value="Delete Checked Variables">
<input type="Submit" name="submit" value="Update Application/Session Variables">
</form>
</body>
<head>
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
</head>
</html>
</CFOUTPUT>


===================================================================================================
SessionEditUpdate.cfm
===================================================================================================

<CFPARAM name="form.sess" default="">
<CFPARAM name="form.app" default="">
<CFPARAM name="form.EditApp" default="">
<CFPARAM name="form.EditSess" default="">
<CFPARAM name="form.submit" default="">

<CFIF submit EQ "Delete Checked Variables">
  <CFIF Len(sess) GT 0>
    <CFLOOP index="var" list="#sess#">
      <CFLOCK timeout="2" throwontimeout="No" name="EditApp" type="EXCLUSIVE">
      <CFSET tmp=StructDelete(session,var)>
      </CFLOCK>
    </CFLOOP>
  </CFIF>

  <CFIF Len(app) GT 0>
    <CFLOOP index="var" list="#app#">
      <CFLOCK timeout="2" throwontimeout="No" name="EditApp" type="EXCLUSIVE">
      <CFSET tmp=StructDelete(application,var)>
      </CFLOCK>
    </CFLOOP>
  </CFIF>

<CFELSEIF submit EQ "Update Application/Session Variables">
  <CFLOOP index="var" list="#Form.EditApp#" delimiters=",">
    <CFSET old=Evaluate("Form.o_app_#var#")>
    <CFSET new=Evaluate("Form.i_app_#var#")>
    <CFIF old NEQ new>
      <cflock timeout="2" throwontimeout="No" name="EditApp" type="EXCLUSIVE">
      <CFSET tmp=StructUpdate(application,var,new)>
      </CFLOCK>
    </CFIF>
  </CFLOOP>
  <CFLOOP index="var" list="#Form.EditSess#" delimiters=",">
    <CFSET old=Evaluate("Form.o_sess_#var#")>
    <CFSET new=Evaluate("Form.i_sess_#var#")>
    <CFIF old NEQ new>
      <cflock timeout="2" throwontimeout="No" name="EditApp" type="EXCLUSIVE">
      <CFSET tmp=StructUpdate(session,var,new)>
      </CFLOCK>
    </CFIF>
  </CFLOOP>
  <CFIF Len(Trim(form.NewApp)) GT 0>
    <cflock timeout="2" throwontimeout="No" name="EditApp" type="EXCLUSIVE">
    <CFSET tmp=StructInsert(application,form.NewApp,form.NewAppVal,"True")>
    </CFLOCK>
  </CFIF>
  <CFIF Len(Trim(form.NewSess)) GT 0>
    <CFLOCK timeout="2" throwontimeout="No" name="EditApp" type="EXCLUSIVE">
    <CFSET tmp=StructInsert(session,form.NewSess,form.NewSessVal,"True")>
    </CFLOCK>
  </CFIF>
</CFIF>

<CFLOCATION URL="SessionEdit.cfm">


Return to the Cold Fusion Tips-N-Tricks topic list