Ask the Training Staff

We have some more good questions to tackle this month. We'll look at Client variables, a pesky CFMAIL problem, and ways to delete Session variables.

Q:  Can you give me a brief explanation of the "Client" variable scope?

A:  The Client variable scope is very similar to the Session scope in that you use it to keep track of certain pieces of data for individual users of your site ("clients"). The biggest difference is that Client variables are not stored in memory as are Session variables.

Client variables are stored physically, either on your server in a database, on your server in the registry, or back in the client's browser as cookies. Because they're stored physically, they don't expire as quickly or get lost if the server is restarted (as Session variables do). This allows you to keep information for individual users and have it available to them when they come back.

A good example would be preference settings for each user. Rather than have users log back in, or reset their preferences every time they come back to your site, you could store them in client variables so that information is available when the user returns.

Like Session variables, Client variables are stored and retrieved using two cookies (CFID and CFTOKEN) as identification. Also like Session variables, you can't use Client variables unless you enable CLIENTMANAGEMENT in your CFAPPLICATION tag (see Listing 1).

Before using Client variables, decide where you want to store them. By default, CF stores all Client variables in the Registry on your server. Never do this! Another choice is to have CF send them back to the user as cookies. This also isn't great because your user can delete or modify them, plus they all get re-sent with every page request (a performance issue for people with modems!). The best solution is to store them in a database.

To have CF store Client variables in a database, go to the Variables section of the CF Administrator. Select the data source you want to use for Client variable storage (create a new data source pointing to an empty database before doing this step). Click "Add" to add this data source to the list of available storage options. You'll need to answer three questions about the data source and then click "Create."

Now your data source will appear in the list of available storage options. You can set it as the default storage option (recommended) or you can have each application use a different storage data source by specifying it in the CFAPPLICATION tag as shown in Listing 1.

Q: Can I use the Client variable scope to store complex data types (like structures or query result sets)?

A:  No and yes (isn't that helpful?). As for the no part, the Client scope can only be used for storing simple values (strings or numbers). However, there is a way around this. You can take a complex data type and turn it into a string using WDDX. To demonstrate this idea, Listing 2 shows three separate CF pages. The full details of WDDX are too great to expound in this column, but the general concept is fairly easy.

First, you must enable client management in your Application.cfm page. Note that CLIENTSTORAGE points to a data source (don't store Client variables in the Registry or as cookies!). The next page, SetClientVar.cfm, runs a query, turns that query into a WDDX packet, and creates a Client variable with the WDDX packet as its value. The third page, ReadClientVar.cfm, takes the value of the Client variable (which is a WDDX packet), turns that packet back into a query record set, and loops over that query to display some results.

Q: After upgrading to CF 4.5, when I send an e-mail with CFMAIL (see Listing 3), the formatting is not retained. Even though I enter empty lines in between two sentences, those empty lines are removed when the mail is sent. Do you know why this happens, and is there any way to avoid it? I didn't have this problem before we upgraded to CF 4.5.

A:  Yes! I hit my head against the wall many times before someone, I forget who, helped out. Go to your CF Admin, and check if you have this turned on: Suppress White Space. That's your culprit. You'll have to turn off this feature and use CFSETTING, or CFSILENT, instead. (This answer was provided by Raymond Camden, principal Spectra compliance engineer for Macromedia.)

Q:  What's the best way to get rid of Session variables I created? Currently, I'm just overwriting them with an empty string (i.e., <CFSET Session.LoggedIn = ""), but they still exist. Is there a better way to do this?

A:  Yes, you can actually delete variables out of the Session scope using the Struct-Delete() function. The Session scope is actually a complex variable type called a Structure (a single variable capable of holding multiple values, each identified by a unique name). Therefore, when you "set" a Session variable, you are actually just creating a "key" or an entry in a structure named "Session." You can also delete all the values stored in the Session scope (or any structure) using the StructClear() function. Listing 4 shows some examples.

Please send your questions about ColdFusion (CFML, CF Server, or CF Studio) to AskCFDJ@sys-con.com. Visit our archive site at www.NetsiteDynamics.com/AskCFDJ.

© 2008 SYS-CON Media