GuidesPage Hit Counter System Page 2

Page Hit Counter System Page 2




The logic behind the include file

  • First get the page name of the current page, using
    servervariables
  • Find the page in counts table
  • If the page does not already exist in the database, insert the
    page with its hits set to 1
  • If the page does exist in the database
       Check
    the hit_date compared to today to see if we need to roll totals or
    just add to the   
  • Update the counts table.

The code behind the include file

Set count =
Server.CreateObject(“ADODB.Connection”)
Set count_rs =
Server.CreateObject(“ADODB.RecordSet”)

count.open
“web_odbc”   ‘our dsn is called
web_obdc

my_page =UCase( Request.ServerVariables(“Path_info”)) ‘get the page name from server
var

                                                                             
‘convert to upper case for
consistancy

sql = “Select * FROM Hits where page_name = ‘” & my_page
& “‘” 
count_rs.Open sql, count, 3, 3, 1

If count_rs.recordcount ‘ none selected first hit on this
page
    sql = “INSERT INTO Hits
(“
    sql = sql & ” page_name, total_hits,
day_hits, month_hits, hit_date) VALUES “
    sql =
sql & “‘” & my_page &”‘, 1, 1, 1, ‘” & Now() &
“‘)”
    count.execute(sql)   ‘do an insert into the table for this
page

else
    t_hits =
CLng(count_rs.fields(“total_hits”))
    d_hits =
CLng(count_rs.fields(“day_hits”))
         ‘ retrieve hits
    m_hits =
CLng(count_rs.fields(“month_hits”))
    last_dt =
count_rs.fields(“hit_date”)
    td_dt =
Now()
    if   Abs(DateDiff(“d”,
last_dt, td_dt) 0 THEN  ‘if the
hit_date doesnt
match

        
d_hits = 0
                          
‘ todays date, reset day_hits to
0

    end if
   
if   Abs(DateDiff(“m”,last_dt, td_dt) 0
then   ‘ same for
month

         
m_hits = 0
    end if
    t_hits
= t_hits + 1
    d_hits = d_hits + 1   ‘
add 1 to all hits
    m_hits = m_hits +
1
    count_rs.fields(“total_hits”) =
t_hits
    count_rs.fields(“day_hits”) = 
d_hits         ‘ update the recordset
   
count_rs.fields(“month_hits”)  = m_hits
   
count_rs.fields(“hit_date”) = Now()     ‘ update hit_date with todays
date

    count_rs.Update  ‘store back to DB
end
if

count_rs.Close   ‘ clean up
work

count.Close
Set count_rs = nothing
Set count =
nothing

%>

Now we just include this file in any page we want the hits
tracked

Latest Posts

Related Stories