2

Consider the following structure for a ColdFusion 9 website:

/root
..../MyApp 
........../Application.cfc 
........../Index.cfm
..../SharedComponents 
........../Common.cfc

Within common.cfc contains all common functions that I'd like to share across multiple applications under root.

One of those functions allows me to dynamically create a session name and assign a value.

<cffunction name="CreateSession" access="remote" returntype="any">
    <cfargument name="strSessionName" required="yes" type="string">
    <cfargument name="strSessionValue" required="yes" type="string">
    <cfset Session["#strSessionName#"] = "#strSessionValue#" />
</cffunction>

I call this function through AJAX as I want this session to be created following a JavaScript process.

This is the expression I use in ColdFusion which is then rendered into an actual URL, which is then called through AJAX.

"#Application.Protocol & "://" & CGI.SERVER_NAME#/SharedComponents/common.cfc?method=CreateSession&strSessionName=TestSessionName&strSessionValue=TestValue"

However, when trying to display this newly created session on Index.cfm, there is nothing displayed.

I tried relocating the common.cfc into the folder MyApp, modifying the ColdFusion expression path (shown above) to reflect the new path, and it works no problem.

This leads me to believe that I can't set a session from a function outside of my application's scope (that being the MyApp folder, where Application.cfc is found)

My question is, is there a way to call a function outside of the Application's scope which can still manage the application's sessions?

MPaul
  • 2,553
  • 3
  • 20
  • 34
  • 2
    Session are tied to the user. `Session["#strSessionName#"]` doesn't really create a session name. It creates a variable in the user's session with a particular name. If you want to share something outside of the application scope, you may want to consider server scope. – James A Mohler Jun 15 '16 at 18:51
  • 2
    Sessions are inherently tied to an application. If a file is not covered by an Application.cfc/cfm, then there is no session. – Leigh Jun 15 '16 at 20:35

1 Answers1

2

This leads me to believe that I can't set a session from a function outside of my application's scope (that being the MyApp folder, where Application.cfc is found)

With your folder structure, you have two ColdFusion applications. You have an application in the folder MyApp and an application in the root folder that the SharedComponents folder belongs too. Unless you opt to use a few hackish steps to share a session between applications, all application have unique session scopes for the users. I wouldn't suggest sharing sessions like this. If you need to share one session scope between all the applications in the one web site, I would reevaluate why you have multiple applications.

In your example, you're having the client make a request directly to the remote function in the sharedComponents folder. The session scope available to the component is a different then the session scope available to the MyApp application.

A way of sharing common code through out all of your applications is to set up a mapping to the location that stores your shared code. Once you have created mapping to the shared coded, you will be able to extend your shared components, or create a wrapper to exposes the shared code. Here is an example that dose both. I have opted to place this new component, all so called Common, into the root of MyApp and I have called my mapping 'mapToSharedComponents'.

component extends="mapToSharedComponents.Common"
{
    remote function wrappedCall(key, value){
        local.common = new mapToSharedComponents.Common();
        common.CreateSession(key,value);
    }
}

http://localhost/MyApp/common.cfc?method=CreateSession&strSessionName=inherited&strSessionValue=test http://localhost/MyApp/common.cfc?method=wrappedCall&key=wrapped&value=test

The two calls will create new key value pairs in MyApp session scope for the calling user. The first call invokes the inherited CreateSession function from your existing component. The call to the new wrappedCall function creates a new instance of your original component and calls the CreateSession function with the arguments passed.

If all your applications are in the same web site, you can skip the mapping and reference the component from the web root. You will still need a component that is apart of the sub folder application. In this case the example component would look like:

component extends="SharedComponents.Common"
{
    remote function wrappedCall(key, value){
        local.common = new SharedComponents.Common();
        common.CreateSession(key,value);
    }
}
Community
  • 1
  • 1
Twillen
  • 1,458
  • 15
  • 22
  • Maybe he's trying to a single-sign on sort of thing? In which case there are other ways to accomplish this. The mapping method is common, though can also have issues. It's not that I don't recommend it, but just note, I would only but very general, functional type methods in a common component. We had a programmer use this mapping style of sharing functions but the functions were sometimes specific to an application. Not only did it get bloated, but then someone changed a function and had no idea where else it was used. I prefer not to share UDFs with other apps. – Leeish Jun 16 '16 at 20:06