Difference between revisions of "ColdFusion Tips & Tricks"
Line 152: | Line 152: | ||
</pre> | </pre> | ||
+ | ==Session Management - How to verify it is working== | ||
+ | By default the Session Timeout is set to 20 minutes. | ||
+ | Here is a sample script to verify the ColdFusion Session variables and ColdFusion Session in general is working. | ||
+ | |||
+ | I'm sure there is an easier way to test this, but here is what I did. I created 2 pages. The first page sets the Session variables. The second page keeps adding to the counter and displaying the count, when the session started, and the current time. | ||
+ | |||
+ | This is handled differently based on whether you are using '''application.cfm''' or '''application.cfc''', so be sure to note which one you are using. | ||
+ | If using '''application.cfm''', you need to make sure your application.cfm file is set to handle session variables. To do this, make sure you have sessionmanagement="yes" like: | ||
+ | <pre style="white-space: pre-wrap"> | ||
+ | <cfapplication sessionmanagement="yes"> | ||
+ | </pre> | ||
+ | If using '''application.cfc''', you need to make sure your application.cfc is set to handle the sessions variables properly. Here is an example for that: | ||
+ | <pre style="white-space: pre-wrap"> | ||
+ | <cfcomponent> | ||
+ | <cfset This.name = "myApplication"> | ||
+ | <cfset This.Sessionmanagement = TRUE> | ||
+ | </cfcomponent> | ||
+ | </pre> | ||
+ | In the first page (ie, cfsessiontest.cfm) add this code: | ||
+ | <pre style="white-space: pre-wrap"> | ||
+ | <cfset SESSION.MyCount = 1> | ||
+ | <cfset SESSION.StartTime = Now()> | ||
+ | Current Count is: <cfoutput>#SESSION.MyCount#</cfoutput><BR /> | ||
+ | Session Started at: <cfoutput>#SESSION.StartTime#</cfoutput><BR /> | ||
+ | <A HREF="cfsessiontest2.cfm">Test Session</A> | ||
+ | </pre> | ||
+ | Now create a new page named cfessiontest2.cfm and add this code: | ||
+ | <pre style="white-space: pre-wrap"> | ||
+ | <cfset SESSION.MyCount = #SESSION.MyCount# + 1> | ||
+ | Current Count is: <cfoutput>#SESSION.MyCount#</cfoutput><BR /> | ||
+ | The current time is <cfoutput>#Now()#</cfoutput> and the Session Started at: <cfoutput>#SESSION.StartTime#</cfoutput><BR /> | ||
+ | <A HREF="cfsessiontest2.cfm">Test Session</A> | ||
+ | </pre> | ||
+ | The first page will load the 2nd page and the 2nd page will keep loading itself when you click on the Test Session link. | ||
[[Category:ColdFusion]] | [[Category:ColdFusion]] |
Revision as of 19:10, 22 August 2012
Contents
Per-Application ColdFusion Mappings
In ColdFusion 8 and above, it is possible to create per-application mappings through your site's Application.cfc file.
Once you have your Application.cfc created, you will insert the following line:
<cfset this.mappings["/test"]="d:\home\yourdomainname.com\wwwroot\test">
On your site though, you would change "/test" to the name of your mapping. IMPORTANT: You need to include the forward slash before the name of your mapping. Also, change "d:\home\yourdomainname.com\wwwroot\test" to the full physical path to the folder you wish to map. Note: The physical path to your FTP root is listed in the "Site Settings" of your control panel at wcp.hostek.com.
To call a template named "testing.cfm" in the "test" directory we just mapped, you would use this line:
<cfinclude template="/test/testing.cfm">
Per-Application Custom Tag Paths
Starting in ColdFusion 8, ColdFusion allows creation of Custom Tag Paths in each site's Application.cfc file. This allows you to create and manage your Custom Tag Paths without having to use CF Administrator (ie. submit a support ticket). The following steps will help you create a Custom Tag Path:
If you do not already have an Application.cfc file in your site's Web root, create one now. Place the following code in the Application.cfc file and save:
<cfcomponent> <cfset THIS.customtagpaths="d:\home\yourdomain.com\wwwroot\customtagfolder"> </cfcomponent>
In this case, you'd replace "yourdomain.com" with your domain name, and "customtagfolder" with the name of the folder you created for your custom tags.
Now you have successfully added your custom tag path. If you have multiple tag paths to add, try using the following code instead:
<cfcomponent> <cfset THIS.customtagpaths=ListAppend(THIS.customtagpaths, "d:\home\yourdomain.com\wwwroot\customtagfolder")> </cfcomponent>
Restart a ColdFusion Application
If you ever need to restart your site's ColdFusion application (to pick up a setting change, etc), you can do so via the ApplicationStop() function in ColdFusion 9+.
To use this you can create a file (ex: restart.cfm) containing the following code:
<cfset ApplicationStop() /> <cflocation url="index.cfm" addtoken="false" />
When you run the file containing that code your ColdFusion Application will be stopped (flushing your application scope), and when the browser is redirected to the index page the application will start again.
Scheduling Tasks in ColdFusion
On occasion shared ColdFusion hosting customers need to setup Scheduled Task to run files on a cycle.
This can be done with the following steps, making use of ColdFusion's <cfschedule> tag:
- Create a file locally using any text editor, save the file with the .CFM extension (example: setschedule_example.cfm).
- Copy and past in the code example below taken from ColdFusion's Documentation and save.
- Replace the data in the example note the following recommendations.
!- Name each file made for creating a schedule with the name of the task so you can reference the task later if needed. Specific naming will also make it more difficult for someone to randomly run the file.
!- If you need to schedule a job to run monthly on any date in the range 28-31, read about how ColdFusion will handle the scheduling in the ColdFuison8 documentation referenced below. - Working example, the code example below is a working tested solution for getting the weather for a specific zip code and the task to email the results.
Scheduled Task
<!-- This sets the initial task, if your just wanting to set the schedule --> <cfschedule action = "update" task = "Weather_Send" operation = "HTTPRequest" url = "http://test.hostek.net/weather_send.cfm" startDate = "7/6/09" startTime = "09:30 AM" interval = "3600" resolveURL = "Yes" requestTimeOut = "600"> <!-- This allows for the task created to be deleted, just uncomment and change the task name. --> <!-- cfschedule action = "delete" task = "Weather_Send"--> <!-- This allows for the task to be paused, just uncomment and change the task name. --> <!-- cfschedule action = "pause" task = "Weather_Send"--> <!-- This allows for the task resumed if paused, just uncomment and change the task name. --> <!-- cfschedule action = "resume" task = "Weather_Send"-->
Weather Collector
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>CF External Weather Checker</title> </head> <body> <cfset zip="12345"> <cfinvoke webservice="http://ws.cdyne.com/WeatherWS/Weather.asmx?wsdl" method="getCityWeatherByZIP" returnvariable="aWeatherReturn"> <cfinvokeargument name="ZIP" value="#zip#"/> </cfinvoke> <cfif aWeatherReturn.temperature EQ ""> <cfoutput> I cannot get the weather for #zip# external http connections must be DOWN.</cfoutput> <cfelse> <cfset message="The temperature for #zip# (Your city) is #aWeatherReturn.temperature# degrees."> </cfif> <cfif message NEQ ""> <cfmail from = "weather.server@test.com" to = "test@test.com" subject = "Weather checker"> #message# </cfmail> </cfif> </body> </html>
Session Management - Handling Bots and Spiders
First and foremost we suggest creating a robots.txt file in the web root of the domain to address two issues. First to control the rate at which the website is being crawled which can help prevent a bot/spider from creating a massive number of database connections at the same time. Second to prevent specific bots from crawling the website. We suggest the following defaults, however you might want to add or remove the user agents denied, and adjust the crawl rate but we suggest nothing lower than 3 seconds.
User-agent: * Crawl-delay: 10 User-agent: Baiduspider Disallow: / User-agent: Sosospider Disallow: /
Next we suggest setting your session timeout specifically lower for bots and spiders. These spiders and bots will crawl a page and when a session (ColdFusion) is created, it will persist during then entire page load. The page fully loaded allows the bot or spider to get the information from the Web page AND allows the session to expire quickly protecting ColdFusion from effects similar to a memory leak.
For examples of how to do this, please see this article: Session Management code examples for the Applicaiton.cfm
If your site utilizes an Application.cfc file instead, see the instructions below, adjusting the timeout to your application's requirements:
Replace within a cfscript block:
THIS.sessionTimeout = createTimeSpan(0,0,60,0);
With:
<!--- This checks if a cookie is created, for bots this will return false and use the low session timeout ---> if (StructKeyExists(cookie, "cfid")){ THIS.sessionTimeout = createTimeSpan(0,0,60,0); } else { THIS.sessionTimeout = createTimeSpan(0,0,0,2); }
Session Management - How to verify it is working
By default the Session Timeout is set to 20 minutes.
Here is a sample script to verify the ColdFusion Session variables and ColdFusion Session in general is working.
I'm sure there is an easier way to test this, but here is what I did. I created 2 pages. The first page sets the Session variables. The second page keeps adding to the counter and displaying the count, when the session started, and the current time.
This is handled differently based on whether you are using application.cfm or application.cfc, so be sure to note which one you are using. If using application.cfm, you need to make sure your application.cfm file is set to handle session variables. To do this, make sure you have sessionmanagement="yes" like:
<cfapplication sessionmanagement="yes">
If using application.cfc, you need to make sure your application.cfc is set to handle the sessions variables properly. Here is an example for that:
<cfcomponent> <cfset This.name = "myApplication"> <cfset This.Sessionmanagement = TRUE> </cfcomponent>
In the first page (ie, cfsessiontest.cfm) add this code:
<cfset SESSION.MyCount = 1> <cfset SESSION.StartTime = Now()> Current Count is: <cfoutput>#SESSION.MyCount#</cfoutput><BR /> Session Started at: <cfoutput>#SESSION.StartTime#</cfoutput><BR /> <A HREF="cfsessiontest2.cfm">Test Session</A>
Now create a new page named cfessiontest2.cfm and add this code:
<cfset SESSION.MyCount = #SESSION.MyCount# + 1> Current Count is: <cfoutput>#SESSION.MyCount#</cfoutput><BR /> The current time is <cfoutput>#Now()#</cfoutput> and the Session Started at: <cfoutput>#SESSION.StartTime#</cfoutput><BR /> <A HREF="cfsessiontest2.cfm">Test Session</A>
The first page will load the 2nd page and the 2nd page will keep loading itself when you click on the Test Session link.