943,925 Members | Top Members by Rank

Ad:
  • ColdFusion Discussion Thread
  • Marked Solved
  • Views: 2371
  • ColdFusion RSS
Jun 19th, 2009
0

How to make user defined function global?

Expand Post »
Hi again,
Okay now I have a Coldfusion question, I have written a user defined function in coldfusion:
coldfusion Syntax (Toggle Plain Text)
  1. <cfscript>
  2. function isSqlInjection(str) {
  3. ....
  4. }
  5. </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.
Similar Threads
Reputation Points: 11
Solved Threads: 0
Newbie Poster
fayola is offline Offline
13 posts
since Jun 2009
Jun 19th, 2009
0

Re: How to make user defined function global?

Ah nevermind I found the Application.cfm file and placing the function in this file made it accessible. Marking as sovled!
Reputation Points: 11
Solved Threads: 0
Newbie Poster
fayola is offline Offline
13 posts
since Jun 2009
Jan 26th, 2010
0

See my comments on this thread

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

Here was the final solution:

[Put in application.cfc:]

ColdFusion Syntax (Toggle Plain Text)
  1.  
  2. <cffunction name="onApplicationStart">
  3.  
  4. . . . snip . . .
  5.  
  6. <cfscript>
  7. application.globalmethods = structnew();
  8. keys = structkeylist(this);
  9. for (i=1; i LTE listlen(keys); i=i+1) {
  10. key = listgetat(keys,i);
  11. if (iscustomfunction(this[key]) and left(lcase(key),2) NEQ "on")
  12. structinsert(application.globalmethods, key, this[key]);
  13. }
  14. application.registerglobalmethods = this.registerglobalmethods;
  15.  
  16. </cfscript>
  17.  
  18. . . . snip . . .
  19.  
  20. </cffunction>
  21.  
  22. <cfinclude template="GlobalFunctions.cfm">
  23.  
  24. <cffunction name="registerglobalmethods" output="no">
  25. <cfset StructAppend(variables, application.globalmethods, true)>
  26. </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):

ColdFusion Syntax (Toggle Plain Text)
  1. <cfset application.registerglobalmethods();>

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

ColdFusion Syntax (Toggle Plain Text)
  1. <cfset StructAppend(variables, application.globalmethods, true)>

when ever a cfc needed to use global functions. But I think this is more readable.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
quickdraw6906 is offline Offline
1 posts
since Jan 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ColdFusion Forum Timeline: Display data in Date Ranges
Next Thread in ColdFusion Forum Timeline: Needed: CF tech part-time for web company.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC