Coldfusion: How to create a hit counter

Discussion in 'Programming' started by lespaul00, Nov 14, 2007.

  1. #1
    How can I create a hit counter for my website?
     
    lespaul00, Nov 14, 2007 IP
  2. unitedlocalbands

    unitedlocalbands Well-Known Member

    Messages:
    246
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    128
    #2
    I use stat counter. Its free, unless you want to build your own

    http://www.statcounter.com/

    I think its way easier though to just get one from someone. It seems like a lot of work.
     
    unitedlocalbands, Nov 14, 2007 IP
  3. lespaul00

    lespaul00 Peon

    Messages:
    283
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I found one from Adobe's download site, that works with my testing server. When I upload it to my website online, it doesn't work. It doesn't make sense to me.

    Here is the code. It seems to automatically update the number of visitors in a txt file named "counter.txt".

    <!--- 
    Worlds Most Basic Counter
    Created by Wil Hale April 2005
    wil@gayleandwil.com
    (please do not remove the above)
    --->
    
    <!---
    Usage:
    Method 1
    - Save this file to your custom tags folder or the folder where you want to use it.
    - Type 
    	<cf_wmbc fontcolor="FFFFFF">
    	on the cfm page where you want the counter to appear.
    	fontcolor defaults to Black and is optional.
    
    Method2
    Copy the contents of this page to the place you want the counter to appear.
    
    Please see my other software at the MM Exchange
    Worlds Most Basic Blog
    Bingo Number Generator
     --->
     
     <!--- Set the default font color incase the attribute was not set in the call --->
    <cfparam name="attributes.fontcolor" default="000000">
    
    <!--- Center it no matter where it is and apply the font style --->
    <div align="center" style="font-weight:bold; color:<cfoutput>#attributes.fontcolor#</cfoutput>">
    	
    	<!--- Check to see if this is the first time this tag has been used if so write it with a value of 0--->
    	<cfif fileexists("#getdirectoryfrompath(path_translated)#counter.txt") is "False">
    	
    	<cffile action="write" file="#getdirectoryfrompath(path_translated)#counter.txt" output="0">
    	</cfif>
    	
    	<!--- Have they been here recently? --->
    	<cfif not isDefined('session.page')>
    		
    		<!--- Set the session so that it wont count them every time they come to the page, 
    		only after they have left and com back after a while. --->
    		<cfset session.page = "onit">
    		
    		<!--- get current hit count and read it into a variable--->
    		<cffile action="read" file="#getdirectoryfrompath(path_translated)#counter.txt" variable="counter">
    		
    		<!--- Delete the file (easiest way to purge the variable --->
    		<cffile action="delete" file="#getdirectoryfrompath(path_translated)#counter.txt">
    		
    		<!--- Rewrite the counter.txt file with the new count in it --->
    		<cffile action="write" file="#getdirectoryfrompath(path_translated)#counter.txt" output="#incrementValue(counter)#">
    	</cfif>
    	
    	<!--- Read the counter variable --->
    	<cffile action="read" file="#getdirectoryfrompath(path_translated)#counter.txt" variable="counter_display">
    	
    	<!--- Show the count --->
    	<cfoutput>
    	  <p><br>
    	  You are vistor number: #counter_display#</p>
    	</cfoutput>
    
    Code (markup):
     
    lespaul00, Nov 15, 2007 IP
  4. unitedlocalbands

    unitedlocalbands Well-Known Member

    Messages:
    246
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    128
    #4
    Make Shift CFMX7 hit counter from scratch! Try it out, Maybe we can improve it a bit.

    This part would go into your application.cfc file. In the OnRequestStart methode.

    You will also need a table in your database called somthing like IPADDRESS,
    and a few columns like IP_ADDR_ID, IP_ADDRESS, START_VISIT, ENDVISIT, VISITS, SESSIONID


    
    <cfset session.sessionid = "#createuuid()#">                            
    <cfquery datasource="#datasource#" name="ipcheck">
    SELECT IP_ADDR_ID, IP_ADDRESS, START_VISIT END_VISIT, VISITS, SESSIONID
    FROM IPADDRESS
    WHERE IP_ADDRESS = '#CGI.REMOTE_ADDR#'
    </cfquery>
    
    <cfif ipcheck.recordcount eq "0">
    <cfquery datasource="#datasource#" name="addip">
    INSERT INTO IPADDRESS (IP_ADDR_ID, IP_ADDRESS, START_VISIT, END_VISIT, VISITS, SESSIONID, CAME_FROM)
    VALUES ('#CREATEUUID()#', '#CGI.REMOTE_ADDR#', #CreateODBCDateTime(NOW())#, NULL, 1, '#session.sessionid#', '#CGI.HTTP_REFERER#')
    </cfquery>
    
    <cfelseif ipcheck.recordcount eq "1">
    <cfquery datasource="#datasource#" name="updateip">
    UPDATE IPADDRESS
    SET START_VISIT = #CreateODBCDateTime(NOW())#,
        END_VISIT = NULL,
        VISITS = #IPCHECK.VISITS# + 1,
        SESSIONID = '#session.sessionid#',
        CAME_FROM = '#CGI.HTTP_REFERER#'
    WHERE IP_ADDR_ID = '#IPCHECK.IP_ADDR_ID#' AND IP_ADDRESS = '#IPCHECK.IP_ADDRESS#'
    </cfquery>
    </cfif>
    
    Code (markup):
    This part goes where ever you want to display the number of hits.

    
    <cfquery datasource="#datasource#" name="count">
    SELECT COUNT(IP_ADDRESS) AS IP_COUNT
    FROM IPADDRESS
    </cfquery>
                    
    <p style="text-align:center;">Hits since Dec 21, 2007<br/><cfoutput>#COUNT.IP_COUNT#</cfoutput></p>
    
    Code (markup):
    This will check the viewers Ip address and see if they have requested your site before. If not then it adds them to the database, If so then is just updates their own number of visits, start_visit, and end_visit times. Also where they came from if its available.

    Enjoy
     
    unitedlocalbands, Dec 27, 2007 IP