Coldfusion Counter

Discussion in 'Programming' started by cjane, Feb 26, 2008.

  1. #1
    where can I find a coldfusion counter code to place on my website, I can't seem to find anything that can help me.
     
    cjane, Feb 26, 2008 IP
  2. unitedlocalbands

    unitedlocalbands Well-Known Member

    Messages:
    246
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    128
    #2
    How would you like it to work, I will build one for you. Then you can see how easy it is to do.

    Would you like it to only count unique hits or every time anyone loads your home page.

    Just let me know what you had in mind and I will build what you need and help you set it up.

    I have one that I built from scratch on my site

    http://www.unitedlocalbands.com

    check it out, its on the left side of the screen
     
    unitedlocalbands, Feb 26, 2008 IP
  3. cjane

    cjane Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    yes, I am looking for something similar to that, I need the

    page loads,
    unique visitors
    first time visitors
    returning vistors
     
    cjane, Feb 27, 2008 IP
  4. unitedlocalbands

    unitedlocalbands Well-Known Member

    Messages:
    246
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    128
    #4
    Ok, just a couple questions and I can get you up and running.

    What Database are you using?

    Do you have an application.cfc file set up in the root directory?

    If you dont have a database to store the users information then I'm not sure how it can be done.

    I use MSSQL with a table set up to store each visitors info,

    Heres what you can do,

    Set up a table in your database ,if you have one, called "USER_COUNTER" or any name you like,

    Then add the following columns:
    USER_IP_ID,
    IP_ADDRESS,
    VISITS

    USER_IP_ID will be your unique key for the table,
    datatype can be set to var-char for the first two and visits should be set to numerical or number.


    If you can get that set up then I will show you the rest. If your not sure or need help just ask, thats why I'm here.
     
    unitedlocalbands, Feb 27, 2008 IP
  5. cjane

    cjane Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I am using MSSQL
    Yes, I do have application.cfm
    Table Setup:
    New table has been created.
    table: USER_COUNTER
    USER_IP_ID,
    IP_ADDRESS,
    VISITS
     
    cjane, Feb 28, 2008 IP
  6. unitedlocalbands

    unitedlocalbands Well-Known Member

    Messages:
    246
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    128
    #6
    Great,

    Are you running on CFMX7 server or not?

    If so, you might want to look into changing your application.cfm to a .cfc file instead. This is something new in cfmx7. I can help you with the crossover as well. maybe you can post the code you use in your application.cfm file then I will build you a new .cfc with the counter code already in it. If not I will just post the code you need to make the counter work.

    Check back after about eight o-clock tonight, I should have it done by then,

    Take care,
     
    unitedlocalbands, Feb 28, 2008 IP
  7. cjane

    cjane Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I am running CFMX6 enterprise on windows 2000. I can't change to .cfc, right now but try posting the code with directions and I will work from there.
     
    cjane, Feb 28, 2008 IP
  8. unitedlocalbands

    unitedlocalbands Well-Known Member

    Messages:
    246
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    128
    #8
    Ok so here is a very basic cf counter. We can build on it as you decied what you want to display. But everything is there to do plenty more with.

    First thing, you can try to put this into your application.cfm file so that everytime someone request your site it will run this code.

    If that doesnt work then try sticking it on your home page.


    this will check to see if the users Ip address is in your table or not. If not they will be added, if so then it wil update the amount of times that Ipaddress has visited your site.
    
    <cfquery datasource="YOUR DATASOURCE" name="ipcheck">
    SELECT USER_IP_ID, IP_ADDRESS, VISITS
    FROM USER_COUNT
    WHERE IP_ADDRESS = <cfqueryparam cfsqltype="cf_sql_varchar" value="#CGI.REMOTE_ADDR#">
    </cfquery>
    
    <cfif ipcheck.recordcount eq "0">
    
    <cfquery datasource="#YOUR DATASOURCE#" name="addip">
    INSERT INTO USER_COUNT (USER_IP_ID, IP_ADDRESS, VISITS)
    VALUES (<cfqueryparam cfsqltype="cf_sql_varchar" value="#CREATEUUID()#">, 
            <cfqueryparam cfsqltype="cf_sql_varchar" value="#CGI.REMOTE_ADDR#">,
            <cfqueryparam cfsqltype="cf_sql_numeric" value="1">)
    </cfquery>
    
    <cfelseif ipcheck.recordcount eq "1">
    
    <cfquery datasource="#YOUR DATASOURCE#" name="updateip">
    UPDATE USER_IPADDRESS
    SET VISITS = #IPCHECK.VISITS# + 1,
    WHERE USER_IP_ID = <cfqueryparam cfsqltype="cf_sql_varchar" value="#IPCHECK.IP_ADDR_ID#"> AND 
          IP_ADDRESS = <cfqueryparam cfsqltype="cf_sql_varchar" value="#IPCHECK.IP_ADDRESS#">
    </cfquery>
    </cfif>
    
    Code (markup):

    Next you can use this to display the results. Place it anywhere you like. And like I said we can expand this to do more math calulation like it you want to see hits per day.


    
    <cfquery datasource="#YOUR DATASOURCE#" name="count">
    SELECT COUNT(IP_ADDRESS) AS IP_COUNT
    FROM USER_COUNT
    </cfquery>
    <cfoutput>
    <table style="width:140px; font-family:Courier; background-color:##000000; color:##CCCCCC;">
     <tr>
       <td>Hits</td>
       <td>#COUNT.IP_COUNT#</td>
     </tr> 
    </table>
    </cfoutput>
    
    Code (markup):
    This code is not tested either but good luck and ask questions if you need any help.
     
    unitedlocalbands, Feb 28, 2008 IP
  9. cjane

    cjane Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I am getting the following error message:

    Error Executing Database Query.
    [Macromedia][SQLServer JDBC Driver][SQLServer]Incorrect syntax near the keyword &apos;WHERE&apos;.

    The error occurred in E:\homepage\Application.cfm: line 203

    201: SET VISITS = #IPCHECK.VISITS# + 1,
    202 : WHERE USER_IP_ID = <cfqueryparam cfsqltype="cf_sql_varchar" value="IPCHECK.IP_ADDR_ID"> AND
    203 : IP_ADDRESS = <cfqueryparam cfsqltype="cf_sql_varchar" value="IPCHECK.IP_ADDRESS">
    204 : </cfquery>
    205: </cfif>



    SQL UPDATE USER_IPADDRESS SET VISITS = 1 + 1, WHERE USER_IP_ID = (param 1) AND IP_ADDRESS = (param 2)
    DATASOURCE DINI
    VENDORERRORCODE 156
    SQLSTATE HY000
     
    cjane, Feb 29, 2008 IP
  10. unitedlocalbands

    unitedlocalbands Well-Known Member

    Messages:
    246
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    128
    #10
    I see two things I did wrong,

    at line 201 delete the comma "," after the 1 and on line 203
    change the "IP_ADDR_ID" to "USER_IP_ID"
     
    unitedlocalbands, Feb 29, 2008 IP
  11. unitedlocalbands

    unitedlocalbands Well-Known Member

    Messages:
    246
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    128
    #11
    Also I see a couple of more errors,

    1) Change all the table names to USER_COUNTER instead of USER_COUNT
    2) in the Update Query I wrote "UPDATE USER_IPADDRESS" and it should be "UPDATE USER_COUNTER"

    Sorry about that,

    Hope everything works out
     
    unitedlocalbands, Mar 1, 2008 IP
  12. cjane

    cjane Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Its working, how can I get the

    visits for a particular day/ visits a day
    unique visits
    first time visits
    returning visits
     
    cjane, Mar 3, 2008 IP
  13. unitedlocalbands

    unitedlocalbands Well-Known Member

    Messages:
    246
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    128
    #13
    You are going to need one more column in your database table called somthing like "VISIT_DATE" set data type to date_time

    Then add this part in red to all the sql statments.

    INSERT STATMENT:
    INSERT INTO (VISIT_DATE, REST OF CURRENT STATMENT

    VALUES (#CreateODBCDateTime(NOW())#, REST OF STATMENTS


    UPDATE STATMENT:
    UPDATE USER_COUNTER
    SET VISIT_DATE = #CreateODBCDateTime(NOW())#,REST OF STATMENT



    After that all you have to do is modifiy the sql statment you use to display you visits results.

    So for "UNIQUE VISITS"
    Use this sql;

    <cfquery.....
    SELECT COUNT(IP_ADDRESS) AS IP_COUNT
    FROM USER_COUNTER
    </cfquery....


    FIRST TIME VISITORS;
    <cfquery.....
    SELECT COUNT(VISITS) AS FIRST_TIME_VISITS
    FROM USER_COUNTER
    WHERE VISITS = '1'
    </cfquery.....

    TOTAL VISITS "RETURNING AND UNIQUE"
    <cfquery....
    SELECT SUM(VISITS) AS NUMBER_OF_VISITS
    FROM USER_COUNTER
    </cfquery...

    i'm having a little trouble with hits for a particular day because I need to figure out how to just look at the date part of the date column and not the time. What happens is the SQL statment will try to pull records for the exaclt time and not just for that day. So I have to do some reading and get back to you on that one.


    Same for hits in a day unless you are ok with an average.
     
    unitedlocalbands, Mar 3, 2008 IP
  14. cjane

    cjane Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Just the visit date is fine, thanks so much!!! is it possible to get the visited websites?
     
    cjane, Mar 3, 2008 IP
  15. unitedlocalbands

    unitedlocalbands Well-Known Member

    Messages:
    246
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    128
    #15
    Do you mean the number of pages they visited on your site?

    Not sure I totaly follow...

    But if you want to know where they came from or where they laned on your site you can use "CGI" variables.

    Example:

    #CGI.REMOTE_ADDR# - This tells you the user Ip Address,
    #CGI.HTTP_REFERER# - this one tells you where they came from,
    #CGI.SCRIPT_NAME# - this tells you what page they are on.

    Theres more, and if you like you can check out this link to the livedocs to see how coldfusion can use them

    http://livedocs.adobe.com/coldfusion/6/CFML_Reference/Expressions5.htm
     
    unitedlocalbands, Mar 4, 2008 IP
  16. cjane

    cjane Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #16
    Ok my last query for this post, in the database I am getting all 1 for VISITS column, shouldn't this display the amount of times the site was visited by the IP_Address?
     
    cjane, Mar 4, 2008 IP
  17. unitedlocalbands

    unitedlocalbands Well-Known Member

    Messages:
    246
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    128
    #17
    That is what it is supposed to do, It sounds like there maybe a problem either with the cfif statement that checks to see if the Ip Address is in the database
    or with the UPDATE SQL statement, If you post the exact code that you are using now I will look at it and try to find the mistake.

    I looked up above at what i posted and see all the problems that I told you about earlier but don't see any that I forgot to mention
     
    unitedlocalbands, Mar 4, 2008 IP
  18. cjane

    cjane Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #18
    <cfquery datasource="DINI" name="ipcheck">
    SELECT USER_IP_ID, IP_ADDRESS, VISITS, VISITED_WEBSITE, VISITED_NAME
    FROM User_Counter
    WHERE USER_IP_ID = <cfqueryparam cfsqltype="cf_sql_varchar" value="#CGI.REMOTE_ADDR#">
    </cfquery>

    <cfif ipcheck.recordcount eq "0">

    <cfquery datasource="DINI" name="addip">
    INSERT INTO User_Counter (USER_IP_ID, IP_ADDRESS, VISITS, VISIT_DATE, VISITED_WEBSITE, VISITED_NAME)
    VALUES (<cfqueryparam cfsqltype="cf_sql_varchar" value="#CREATEUUID()#">,
    <cfqueryparam cfsqltype="cf_sql_varchar" value="#CGI.REMOTE_ADDR#">,
    <cfqueryparam cfsqltype="cf_sql_numeric" value="1">,
    <cfqueryparam cfsqltype="cf_sql_timestamp" value="#CreateODBCDateTime(NOW())#">,
    <cfqueryparam cfsqltype="cf_sql_varchar" value="#CGI.HTTP_REFERER#">,
    <cfqueryparam cfsqltype="cf_sql_varchar" value="#CGI.SCRIPT_NAME#">)
    </cfquery>

    <cfelseif ipcheck.recordcount eq "1">

    <cfquery datasource="DINI" name="updateip">
    UPDATE User_Counter
    SET VISITS = IPCHECK.VISITS + 1, VISIT_DATE = CreateODBCDateTime(NOW()), VISITED_WEBSITE = CGI.HTTP_REFERE, VISITED_NAME = CGI.SCRIPT_NAME
    WHERE USER_IP_ID = <cfqueryparam cfsqltype="cf_sql_varchar" value="#IPCHECK.USER_IP_ID#"> AND
    USER_IP_ID = <cfqueryparam cfsqltype="cf_sql_varchar" value="#IPCHECK.IP_ADDRESS#">
    </cfquery>
    </cfif>
     
    cjane, Mar 5, 2008 IP
  19. unitedlocalbands

    unitedlocalbands Well-Known Member

    Messages:
    246
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    128
    #19
    The parts in red are what you will need to change your code to,
    So instead of what you have there now, put this stuff in red there instead.

    
    <cfquery datasource="DINI" name="ipcheck">
    SELECT USER_IP_ID, IP_ADDRESS, VISITS, VISITED_WEBSITE, VISITED_NAME
    FROM User_Counter
    WHERE [COLOR="Red"]IP_ADDRESS [/COLOR]= <cfqueryparam cfsqltype="cf_sql_varchar" value="#CGI.REMOTE_ADDR#">
    </cfquery>
    
    <cfif ipcheck.recordcount eq "0">
    
    <cfquery datasource="DINI" name="addip">
    INSERT INTO User_Counter (USER_IP_ID, IP_ADDRESS, VISITS, VISIT_DATE, VISITED_WEBSITE, VISITED_NAME)
    VALUES (<cfqueryparam cfsqltype="cf_sql_varchar" value="#CREATEUUID()#">, 
    <cfqueryparam cfsqltype="cf_sql_varchar" value="#CGI.REMOTE_ADDR#">,
    <cfqueryparam cfsqltype="cf_sql_numeric" value="1">, 
    <cfqueryparam cfsqltype="cf_sql_timestamp" value="#CreateODBCDateTime(NOW())#">,
    <cfqueryparam cfsqltype="cf_sql_varchar" value="#CGI.HTTP_REFERER#">,
    <cfqueryparam cfsqltype="cf_sql_varchar" value="#CGI.SCRIPT_NAME#">)
    </cfquery>
    
    <cfelseif ipcheck.recordcount eq "1">
    
    <cfquery datasource="DINI" name="updateip">
    UPDATE User_Counter
    SET [COLOR="red"]VISITS = IPCHECK.VISITS + 1, 
        VISIT_DATE = <cfqueryparam cfsqltype="cf_sql_timestamp" value="#CreateODBCDateTime(NOW())#">,
        VISITED_WEBSITE = <cfqueryparam cfsqltype="cf_sql_varchar" value="#CGI.HTTP_REFERER#">, 
        VISITED_NAME = <cfqueryparam cfsqltype="cf_sql_varchar" value="#CGI.SCRIPT_NAME#">[/COLOR]
        
    WHERE USER_IP_ID = <cfqueryparam cfsqltype="cf_sql_varchar" value="#IPCHECK.USER_IP_ID#"> AND 
    [COLOR="red"]IP_ADDRESS [/COLOR]= <cfqueryparam cfsqltype="cf_sql_varchar" value="#IPCHECK.IP_ADDRESS#">
    </cfquery>
    </cfif>
    
    Code (markup):
     
    unitedlocalbands, Mar 5, 2008 IP
  20. cjane

    cjane Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #20
    It didn't work, after the updates, by the way thank you so much for your help!!! the details, and step by step process!!! you are a valued tool to digital point! thanks again
     
    cjane, Mar 5, 2008 IP