Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Application.cfc vs. Application.cfm 1

Status
Not open for further replies.

imstillatwork

IS-IT--Management
Sep 26, 2001
1,605
US
Should I think about using Application.cfc instead of .cfm? I want to keep up-to-date and cold fusion is providing a more OOP like language with every release. I feal myself getting a little to comfy with what I know, and not keeping up with the new.

Can someone show me an example of how to:

* enable session management

* run a bit of code on every .cfm

* run a bit of code at application start up only

Thanks, I am also reading the docs, but a nice real world example is so much nicer for quik learning.

 
This is something that I have been battling with too. I have used cookies for years, and now I want to switch to sessions. I have been learning CFC structure as well, and am really wanting to implement it across projects. I have also read documents, forums, etc. However real world examples are easier to follow IMO.
 
Yep, I got away from cookies years ago, and haven't touched MS Access for at leat 3 years. I just want to make sure I'm up on it. Nothing worse than the guys that refuse to learn a new way of doing something just because the old way 'still works'. I should probably start with playing around with simple cfcs in genreral, then I can pick up the application specifics easier.

 
I think in order to use some of the new functionality you need to use .cfc. (onsessionstart, onapplicationstart) and the like.

This seems like a pretty big change in the way CF works. I don't have any experience in 7 yet, but, I would say get familure with it, it's probably here to stay. It may even phase out application.cfm at some point.

Then on the other hand, it may be a tease like CF5 and dsnless connections. [mad]

We've heard that a million monkeys at a million keyboards could produce the complete works of Shakespeare; now, thanks to the Internet, we know that is not true.
 
this is a bit of a biggy and I never got round to working out how to display code nicely in these forums :)

I've used the new application.cfc and related framework to track the amount of live sessions in an application scope array - ..

<cfcomponent namespace="Application.cfc" output="Yes" >

<cfscript>
//set attributes normally set in the cfapplication tag.
this.Name = "appnane";
this.applicationTimeout = createTimeSpan(7,0,0,0);
this.sessionManagement = "yes";
this.sessionTimeout = CreateTimeSpan(0, 0, 0,30);
this.scriptProtect = "all";
this.setDomainCookies = "true";
this.setClientCookies = "true";//defaults to true but incase
</cfscript>

<cffunction name="onApplicationStart"
returnType="boolean" output="Yes">
<cfscript>
//create structure to contain a reference to all users...
//here you would put initialisation code
APPLICATION.allUsers = arrayNew(1);
</cfscript>
<cfreturn true />
</cffunction>

<cffunction name="onSessionStart">
<cfscript>
//session initilisation
SESSION.user = structnew();
SESSION.user.sessionID = createUUID();
SESSION.user.started = now();
SESSION.user.isLoggedIn = false;
</cfscript>

<cflock scope="Application" timeout="5" type="Exclusive">
<cfscript>
//add new session to our global structure....
arrayAppend(Application.allUsers,SESSION.user);
</cfscript>
</cflock>
<cfreturn true />
</cffunction>

<cffunction name="onRequestStart" returnType="boolean">
<cfargument type="String" name="targetPage" required=true/>
<cfscript>
//not locked - not worried about a race condition
SESSION.user.lastPageRequested = arguments.targetPage & "?" & cgi.query_string;
SESSION.user.timeOfLastRequest = now();
</cfscript>
<cfreturn true>
</cffunction>

<!--- when session ends remove this session from our application scope structure! --->
<cffunction name="onSessionEnd" returnType="void">
<cfargument name="SessionScope" required=True/>
<cfargument name="ApplicationScope" required=False/>

<!--- find the session which is ending so we can remove it from our structure --->
<cfloop from="1" to="#arrayLen(Arguments.ApplicationScope.allUsers)#" index="i" >
<cfif Arguments.ApplicationScope.allUsers.sessionID eq arguments.SessionScope.user.sessionID >
<!--- remove from the structure (lock?) --->
<cfset ArrayDeleteAt(Arguments.ApplicationScope.allUsers, i) />
<cfbreak>
</cfif>
</cfloop>
</cffunction>

</cfcomponent>

this code works but may need reviewing to ensure there are no nasty race conditions. The new application framework is cool but there are a lot of gotchas to be aware of such as code just not working if there is an error in some of the method (and *not* ) throwing an error. There is a whole new section of documentation in livedocs on this - I would recommend reading it carefully as I tripped up a lot of things which are in the docs but who really RTFM? :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top