<!--- SIMPLECOUNTER.CFM (may be renamed to whatever you want) by brendan avery - ba@brendanavery.com - http://www.brendanavery.com
---- description a simple hit counter example. use only in the context of <cfmodule> or as a custom-tag.
---- requirements <CFFILE> must be enabled.
---- attributes "filename" - string - required - this is the filename to use for the counter data. this filename should be the name of the file. if the full path is not used, the file will be read/written in the directory of the base template. to have different counters for different pages, just use different filenames. examples usage:
<cf_simplecounter filename="mycounter.txt"> <cf_simplecounter filename="mycounter2.txt"> <cf_simplecounter filename="subdir\mycounter.txt"> <cf_simplecounter filename="C:\mysite\mycounter.txt"> <cf_simplecounter filename="#expandpath('mycounter.txt')#">
"result" - string - optional - this is the name of a variable to return which will have a value equal to the NEW number of hits in the counter, or, in the event of an error, the value 0. example usage:
<cf_simplecounter filename="C:\mysite\counter.txt" result="mynumber"> total hits: <cfoutput>#mynumber#</cfoutput>
---> <cfparam name="attributes.filename"> <cfparam name="attributes.result" default=""> <cfset attributes.filename=#ExpandPath(attributes.filename)#> <cftry> <cffile action="READ" file="#attributes.filename#" variable="currentvalue"> <cfset currentvalue=#Int(Trim(currentvalue))#> <cfcatch> <cfset currentvalue=0> </cfcatch> </cftry> <cfset currentvalue=#currentvalue#+1> <cftry> <cffile action="WRITE" file="#attributes.filename#" output="#currentvalue#" addnewline="No"> <cfcatch> <cfset currentvalue=0> </cfcatch> </cftry> <cfif attributes.result is not ""> <cfset "caller.#attributes.result#"=#currentvalue#> </cfif>