Databaseless hit counter with visitor information
Databaseless hit counter with visitor information

You will need to create four files:
  hit_counter.txt,
  visitor_log.txt,
  hit_counter.cfm,
  log_viewer.cfm

And download the zipfile of the images from here:
IMAGES

First create a folder on your site and name it hit_counter.
Example:
http://www.yoursite.com/hit_counter

Then copy all of the files you created into this folder.

Then create a folder inside that called images.
Example:
http://www.yoursite.com/hit_counter/images

Extract the images from the zip files into this folder


So now you should have hit_counter.txt, visitor_log.txt, hit_counter.cfm, log_viewer.cfm in the hit_counter folder and image_0.jpg thru image_9.jpg in the images folder.

NOTE: the page hit_counter.txt must have only a "0" on it (or any number you want your counter to begin with)


Now in the hit_counter.cfm enter the following code:

<!-----retreive number--------->
<cffile action="read" file="D:\webserver\joeyswvbest.com\www\test\hit_counter.txt" variable="hit_count">
<!-------end retreive------->
<!------add one to the hit count------->

<cfset hit_count = NumberFormat(hit_count, "0000000") + 1>
<!--------end add one to the hit count------->
<!--------loop thru and assign image names according the number------->

<cfloop index="i" from="1" to="7">
   <cfset "image_#i#" = MID(NumberFormat(hit_count, "0000000"),i,1)>
</cfloop>

<!--------end loop thru and assign image names according the number------->
<!-------display counter-------->

<table border="1" cellpadding="0" cellspacing="0">
<cfoutput>
<tr><td nowrap>
<img src="http://www.yoursite.com/hit_counter/images/image_#image_1#.jpg">
<img src="http://www.yoursite.com/hit_counter/images/image_#image_2#.jpg">
<img src="http://www. yoursite.com/hit_counter/images/image_#image_3#.jpg">
<img src="http://www. yoursite.com/hit_counter/images/image_#image_4#.jpg">
<img src="http://www. yoursite.com/hit_counter/images/image_#image_5#.jpg">
<img src="http://www. yoursite.com/hit_counter/images/image_#image_6#.jpg">
<img src="http://www. yoursite.com/hit_counter/images/image_#image_7#.jpg">

</td></tr>
</cfoutput>
</table>
<!-------end display counter-------->
<!------write the updated hit count to the text file------->

<cffile action="write" file="D:\webserver\joeyswvbest.com\www\test\hit_counter.txt" output="#NumberFormat(hit_count, "0000000000")#" addnewline="no">
<!------end write the updated hit count to the text file------->
<!-------reteive visitor information------>

<CFSET address = cgi.remote_addr>
<CFSET host = cgi.remote_host>
<CFSET referer = cgi.http_referer>
<CFSET agent = cgi.http_user_agent>
<CFSET page = cgi.script_name>
<CFIF NOT Len(host)>
    <CFSET host = "unknown">
</CFIF>
<CFIF NOT LEN(referer)>
    <CFSET referer = "unknown">
</CFIF>

<!-------end reteive visitor information------>
<!-------write visitor information to file------>

<cffile action="append"
file="D:\webserver\joeyswvbest.com\www\test\visitor_log.txt"
output="#address#, #host#, #referer#, #agent#, #page#, #CreateodbcDateTime(Now())#"
addnewline="yes">

<!-------end write visitor information to file------>

This code will retrieve the last hit number add 1 to it and display it on your webpage, plus write it back to the hit_count.txt file.


Now add this code to the log_viewer.cfm to retrieve and view the log file the previous code wrote:

<!--------reteive log file information--------->
<cffile action="read"
file="D:\webserver\joeyswvbest.com\www\test\visitor_log.txt" variable="visitor_log">

<!--------end reteive log file information--------->
<!-------separate each vistors record---------->

<cfset file_array = ArrayNew(1)>
<cfloop index="rc" list="#visitor_log#" delimiters=",#chr(10)#">
   <cfset rc = ArrayAppend(file_array, rc)>
</cfloop>

<!-------end separate each vistors record---------->
<!-----set variables to display each entry inside each record------->

<cfset a = 1>
<cfset b = 2>
<cfset c = 3>
<cfset d = 4>
<cfset e = 5>
<cfset f = 6>

<!-----end set variables to display each entry inside each record------->
<!----------get total record and divide by 6 (6 being 6 entries in each record)---->

<cfset loop_end = arrayLen(file_array)/6>
<!----------end get total record and divide by 6 (6 being 6 entries in each record)---->
<!------loop thru each record and show entries------>

<cfloop index="i" from="1" to="#loop_end#">
<cfoutput>

<strong>Visitor Number: #i#</strong><br>
address: #file_array[a]#<br>
Host: #file_array[b]#
<br>
referer: #file_array[c]#
<br>
Agent: #file_array[d]#
<br>
Page: #file_array[e]#
<br>
Time: #file_array[f]#
<br>
</cfoutput>
<cfset a = a + 6>
<cfset b = b + 6>
<cfset c = c + 6>
<cfset d = d + 6>
<cfset e = e + 6>
<cfset f = f + 6>

</cfloop>
<!------end loop thru each record and show entries------>



All ColdFusion Tutorials By Author: Jeff Sheppard