EasyCFM.COM ColdFusion Forums / ColdFusion Applications / application.cfm

   Reply to Discussion | New Discussion << previous || next >> 
Posted By Discussion Topic: application.cfm

book mark this topic Printer-friendly Version  send this discussion to a friend  new posts last

cvu
02-27-2006 @ 2:42 PM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
New Member
Posts: 4
Joined: Feb 2006

Is there a way to avoid setting session variables (such as session.dsn) multiple times in multiple application.cfm files.  In other words, how to set session variables only once even you have multiple application.cfm files in different directories.

JJfutbol
02-27-2006 @ 2:50 PM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
Moderator
Posts: 1250
Joined: Nov 2004

Great question cvu. Never thought of a situation like that. Unless your application.cfm files that are in your subdirectories, not the parent one, unless they are including the application.cfm files from higher up you won't have access to those application or session variables.

For example lets say you have application.cfm in the root directory where you define your cfapplication tag and set some application and session vars. Then in that directory you have a folder called student with its own application.cfm. That file must have this:

<cfinclude template="../application.cfm">

For you to have access to the 'cfapplication' you defined in the root directory level application.cfm, does that make sense?? So if this is not going on you won't have to worry but if you still want to create session vars without having to set them each time a request is made, do the following:

<!--- if dsn is NOT in session scope--->
<cfif NOT structKeyExists(Session, "DSN")>
     <!--- then create it --->
     <cfset Session.DSN = "dbHello">
</cfif>

That code above will only set the Session variable once as the next time you make a request that will if statement will prove true as the var exists, so it will  never reset, until your session times out.

----------------------------------------------------
Need FREE CF applications?? Then my site has it all, http://www.javier-julio.com/development/coldfusion/downloads/ In time I will be including many more free CF apps and ASP ones as well. Currently working on a fully accessible Forum, which validates as full CSS and XHTML 1.0 Strict. It will be open source and available to all.

This message was edited by JJfutbol on 2-27-06 @ 2:51 PM

cvu
02-28-2006 @ 10:27 AM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
New Member
Posts: 4
Joined: Feb 2006

with the <cfinclude ...> tag, the application.cfm files would override each other.  For example: if you put the <cfinclude template="../parent/application.cfm"> at the end of the ../subdir/application.cfm.  The ../parent/application.cfm would override the one in subdir.  In other words, only one application.cfm is processed even with the <cfinclude ...> tag.

JJfutbol
02-28-2006 @ 10:36 AM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
Moderator
Posts: 1250
Joined: Nov 2004

Not sure I follow cvu because I don't see a reason why you would do that. If I'm in a subdirectory, the application.cfm in that subdirectory would be including the one in the directory above it. Then if theres another higher directory level, the application.cfm in that directory would include from the one above. From what I think you might be saying you have it the other way around. The reason though why you would want to include application.cfm is so that your parent applicaiton.cfm that has the cfapplication tag 'persists' (can't think of another word) to other folders because if not you will not be able to access your application and sessions variables. Does that make sense? I hope I explained myself well if not let me know and I will try better.

You do have to worry about your subdirectories that have application.cfm because if they are not including the higher up application.cfm files (specifically the parent one) that has the cfapplication tag you will not be able to access those session and application vars. So what you have to do is go through all application.cfm files and check to make sure its going to the parent to one to get access to that session var. If not you will have to set it in those application.cfm files in your subdirectories that depend on that session var to exist. I hope I haven't lost ya, just trying my best to answer your issue I just don't understand the entire scope of it I just hope my explanation is proving useful. Smile  If not I will stop here.

----------------------------------------------------
Need FREE CF applications?? Then my site has it all, http://www.javier-julio.com/development/coldfusion/downloads/ In time I will be including many more free CF apps and ASP ones as well. Currently working on a fully accessible Forum, which validates as full CSS and XHTML 1.0 Strict. It will be open source and available to all.

This message was edited by JJfutbol on 2-28-06 @ 10:37 AM

cvu
02-28-2006 @ 12:10 PM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
New Member
Posts: 4
Joined: Feb 2006

Sorry I lost you.  Consider this senario:
In ../parent/application.cfm

<cfapplication name="test1" clientmanagement="Yes" sessionmanagement="Yes" setclientcookies="Yes" sessiontimeout="#CreateTimeSpan(0,24, 0, 0)#" applicationtimeout="#CreateTimeSpan(0,24, 0, 0)#">


<cfif NOT structKeyExists(Session, "db_source")>
     <cfset Session.db_source = "db_test1">
</cfif>

in ../parent/subdir/application.cfm

<cfinclude template="../parent/application.cfm">

<cfapplication name="test2" clientmanagement="Yes" sessionmanagement="Yes" setclientcookies="Yes" sessiontimeout="#CreateTimeSpan(0,2, 0, 0)#" applicationtimeout="#CreateTimeSpan(0,2, 0, 0)#">


<cfif NOT structKeyExists(Session, "var")>
     <cfset Session.var = "test_var">
</cfif>

When request a page in subdir, the <cfapplication name="test2" ... in ../parent/subdir/application.cfm would override the <cfapplication name="test1" ... in the ../parent/application.cfm, therefore only session.var is set and not the session.db_source.  If I take out the <cfapplication name="test2"... out both session variables are set but would have the same session parameters as declared in the parent application.cfm such as expired time and ....etc.  I hope this would clear you confusion.

JJfutbol
02-28-2006 @ 12:30 PM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
Moderator
Posts: 1250
Joined: Nov 2004

Ah ok I see you what you are saying, you are defining a new application again. The example I was talking about above was not defining a new cfapplication again in the sub directory application.cfm files because that way they 'share' their persisting variables. Which you seem to understand fine so I don't think I really understand what the problem is because using the if statement will only create the session variable if doesn't already exist.

Unless I have a new or seperate application buried underaneath another application I will define a new cfapplication but I won't ever include it though as that would cause the overriding you are talking about.

In the case of the session var problem you were having are your application.cfm files doing what you showed above?? That they are including the parent application.cfm and creating a new cfapplication?? I take you need to make sure those session vars exist in the subdirectory files correct?? If thats case the issue is that you are defining the new cfapplication which needs to be taken out, and then you will have access to the session variable you defined higher up which I believe is session.var [correction] its session.db_source

But as long as you stick to using that structKeyExists you should be fine. Smile  From the talented devs I work with, its what they use. I think that isDefined takes a bit longer to run and its much faster to work with structKeyExists, probably makes more sense to them as variable scopes in CF are structures. But if you want feel free to use isDefined too, that will work the same, just probably not as efficient.

<cfif NOT isDefined("Session.DSN")>
      <cfset Session.DSN = "dbsomething">
</cfif>

----------------------------------------------------
Need FREE CF applications?? Then my site has it all, http://www.javier-julio.com/development/coldfusion/downloads/ In time I will be including many more free CF apps and ASP ones as well. Currently working on a fully accessible Forum, which validates as full CSS and XHTML 1.0 Strict. It will be open source and available to all.

This message was edited by JJfutbol on 2-28-06 @ 12:35 PM


Website Designed and Developed by Pablo Varando.