954,585 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to make user defined function global?

Hi again,
Okay now I have a Coldfusion question, I have written a user defined function in coldfusion:

<cfscript>
function isSqlInjection(str) {
  ....
 } 
</cfscript>


Now I am a complete newbie to coldfusion and don't want to have to include this function in every page when I have to do this validation, but would rather have this as a global function to the whole application.
I've inherited this project so not completely 100% on how this application is set up, but if I can get an idea where to place this code to make it global that would be great.

I noticed an _app.cfm file that had some default values assigned and tried placing the function in this file, but that did not work.

fayola
Newbie Poster
13 posts since Jun 2009
Reputation Points: 11
Solved Threads: 0
 

Ah nevermind I found the Application.cfm file and placing the function in this file made it accessible. Marking as sovled!

fayola
Newbie Poster
13 posts since Jun 2009
Reputation Points: 11
Solved Threads: 0
 

See my comments at:
Creating Globally Accessible User Defined Functions In ColdFusion (Safer Version)

Here was the final solution:

[Put in application.cfc:]

<cffunction name="onApplicationStart">

. . . snip . . .

<cfscript>
application.globalmethods = structnew();
keys = structkeylist(this);
for (i=1; i LTE listlen(keys); i=i+1) {
key = listgetat(keys,i);
if (iscustomfunction(this[key]) and left(lcase(key),2) NEQ "on")
structinsert(application.globalmethods, key, this[key]);
}
application.registerglobalmethods = this.registerglobalmethods;

</cfscript>

. . . snip . . .

</cffunction>

<cfinclude template="GlobalFunctions.cfm">

<cffunction name="registerglobalmethods" output="no">
<cfset StructAppend(variables, application.globalmethods, true)>
</cffunction>


Finally, in any cfc that uses a global function, call the register function, then you can use any global function (with no scope prefix):

<cfset application.registerglobalmethods();>


Now, you could skip the register function and just use:

<cfset StructAppend(variables, application.globalmethods, true)>


when ever a cfc needed to use global functions. But I think this is more readable.

quickdraw6906
Newbie Poster
1 post since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You