Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hit Counter Problems

Status
Not open for further replies.

sevex

Programmer
Joined
Sep 18, 2001
Messages
74
Location
CA
I have a few sites that use very simple coldfusion hit counters. Every so often though, the file that holds the number of hits will get filled up with html or other stuff that shouldn't be in there, causing an error. I can't for the life of me figure out why it does this. Below is my code, if anyone has any suggestions, I'll be eternally grateful.

<!--- Set directory path for hits file --->
<cfset hitsfile=ExpandPath(&quot;sitehits.txt&quot;)>

<!--- Read hits from hits file --->
<cffile action=&quot;Read&quot; file=#hitsfile# variable=&quot;sitehits&quot;>


<cfif IsDefined(&quot;cookie.counted&quot;) is False>
<!--- If cookie is not found, accumulate hits, update file --->
<cfset sitehits=#sitehits# + 1>
<cfif #IsNumeric(sitehits)#>
<cffile action=&quot;Write&quot; file=#hitsfile# output=#sitehits#>
</cfif>
<cfcookie name=&quot;counted&quot;>
</cfif>

<!--- Display hits --->
<cfoutput>#sitehits#</cfoutput>
 
yeah, it's getting filled with error html, but why would that happen in the first place?
 
Yes, use <cflock>. I would also modify the script to be shown as below:


=== START CODE EXAMPLE ===
[COLOR=666666]<!--- Set directory path for hits file --->[/color]
<cfset hitsfile=ExpandPath(&quot;sitehits.txt&quot;)>

[COLOR=666666]<!--- Read hits from hits file --->[/color]
<cflock name=&quot;#hitsfile#&quot; type=&quot;READONLY&quot; timeout=&quot;5&quot;>
<cffile action=&quot;Read&quot; file=#hitsfile# variable=&quot;sitehits&quot;>
</cflock>

[COLOR=666666]<!--- If cookie is not found, read file, accumulate hits, update file --->[/color]
<cfif not IsDefined(&quot;cookie.counted&quot;)>
[COLOR=666666]<!--- Check to make sure string is numeric, if not set it to 1 --->[/color]
<cfif #IsNumeric(sitehits)#>
<cfset sitehits=#sitehits# + 1>
<cfelse>
<cfset sitehits=1>
</cfif>

[COLOR=666666]<!--- Write the new hits to the file --->[/color]
<cflock name=&quot;#hitsfile#&quot; type=&quot;EXCLUSIVE&quot; timeout=&quot;5&quot;>
<cffile action=&quot;Write&quot; file=#hitsfile# output=#sitehits#>
</cflock>

[COLOR=666666]<!--- Set the users cookie to counted --->[/color]
<cfcookie name=&quot;counted&quot;>
</cfif>

[COLOR=666666]<!--- Display hits --->[/color]
<cfoutput>#sitehits#</cfoutput>
=== END CODE EXAMPLE === - tleish
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top