Why is this not working?

I have the following code in my application.cfm

application.cfm
------------------

<cfif NOT IsDefined("SESSION.init")>
<cfquery name="listenv" datasource="database">
select *
from table1
where struser = test
</cfquery>

<cfset SESSION.field1 = #listenv.field1#>
<cfset SESSION.field2 = #listenv.field2#>
<cfset SESSION.field3 = #listenv.field3#>
<cfset SESSION.field4 = #listenv.field4#>

<cfset SESSION.init = true>
</cfif>

Then in my index.cfm file I'm just outputting the above SESSION variables for testing yet gets displayed except the SESSION.init variable. Why?
What's wrong with my syntax?

index.cfm
------------

<cfoutput>
SESSION.init: #SESSION.init#<br>

SESSION.field1: #SESSION.field1#<br>
SESSION.field2: #SESSION.field2#<br>
SESSION.field3: #SESSION.field3#<br>
SESSION.field4: #SESSION.field4#<br>
</cfoutput>

--Original Message Text---
From: Jim Davis
Date: Tue, 7 Oct 2003 01:18:57 -0400

I would do this where you log into the application.  You can do this in
Application.cfm but you must do two things:

1) Store the query results in a persistent scope (if this is user
information use the "Session" scope, if it's information applicable to
the whole application and all users use the "Application" scope).  This
will allow the information to be maintained across multiple requests
without being destroyed.

2) You need to check and see if the information has already been set and
only set it if it needs to be.  The Application.cfm template is run for
EVERY SINGLE request - unless you do this kind of check you'll be
running the code over and over.

Something like this (in the Application.cfm).  This assumes that you're
using the session scope on CFMX - if you're using CF 5 or below you
should lock the accesses to the session scope to prevent server
instability:

<cfif NOT IsDefined("Session.Init")>
<cfquery name="myQuery"...>
Your query
</cfquery>
<!--- Do whatever you have to do with your variables
You might only set the results of the query into the session
scope like this: --->
<cfset Session.myQuery = myQuery>
<!--- Now set your marker variable - once this is done this
section of code won't run on the next request for this user --->
<cfset Session.init = true>
</cfif>

There are actually many other ways to do this.  One that you might look
into if you're only caching the query (and using it as it's returned) is
to cache the query using the CachedWithin and CachedAfter attributes of
CFQuery.  Once you set these if the SAME EXACT query is run again before
the refresh period CF will pull the information from the cache and not
bother hitting the database.

This makes for VERY fast access of the cached query and a very simple
page (no marker variables or persistent scopes needed).  There are
limitations - you read the docs on this, but it may be the way to go.

Jim Davis

-----Original Message-----
From: Bushy [mailto:[EMAIL PROTECTED]
Sent: Monday, October 06, 2003 11:49 AM
To: CF-Talk
Subject: re: application.cfm

HI,

I need to load my database fields into session and cookie variables for
my application. The catch is I only need <cfquery the database once upon
initial log in via Windows NT
authentication. I don't want the datbase queried if a "refresh" is
performed. Where or how can I do this. Sould the <cfquery be in my
application.cfm off the main root of the
website?

/inetpub/wwwroot/website/application.cfm


[Todays Threads] [This Message] [Subscription] [Fast Unsubscribe] [User Settings]

Reply via email to