Page Hit Counter System

Monday Jan 11th 1999 by ServerWatch Staff
Share:

I had a client that just wanted a simple report he could easily see in real time, that would show him only what he wanted to see. He had 5 requirements...

by Bob Dombroski

I had a client that just wanted a simple report he could easily see in real time, that would show him only what he wanted to see. He had 5 requirements.

  • How many hits total for any page and all pages.
  • How many hits for any and all pages today.
  • How many hits for any and all pages this month.
  • How many users are currently browsing the site.
  • The number of highest simultaneuos connections throughout the day.

I am sure there are many prepackaged solutions which may have worked, but the task is simple enough to code in ASP. I decided to use a database solution to track the hits.

The database table (Access Database) called hits

  1. page_name - text field max of 100   (contains the page name)
  2. total_hits     - long   (contains the total hits on page since its inception)
  3. day_hits      - long   (contains the total hits on page today)
  4.   month_hits - long   (contains the total hits on page this month)
  5.   hit_date     - Date/Time  (the date of the current day & month hits)

table.jpg

Pretty simple so far...and it doesn't get any harder!

I created an include file counter.inc which is included in every page I want counted. For my client, he did not want the adminstration pages to inflate his hit counts so the include file would not be within any of the admin pages.

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 < 1 then   ' 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

The hit report page is straightforward. We basically loop thru the hits table and display each  pages results and tally the grand total for the 3 categories( daily, monthly and total). Since we dont rotate all totals automatically we must check the hit_date and todays date to verify if the month and day totals need to be reported.

<HTML>
<TITLE>HIT COUNTER</TITLE>
<BODY>
<table border="1" cellpadding="3" cellspacing="0" width="80%" align="center">

<tr>
<td>
<font size="1" face="Arial"><center>PAGE NAME</font>
</td>
<td>
<font size="1" face="Arial"><center>TOTAL HITS</font>
</td>
<td>
<font size="1" face="Arial"><center>TODAY'S HITS</font>
</td>
<td>
<font size="1" face="Arial"><center>THIS MONTHS HITS</font>
</td>
</tr>

<% Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open("web_odbc")
Set RS = Conn.Execute("SELECT * FROM hits  ORDER BY total_hits DESC")

total = 0        ' Three variables to running totals
T_MONTH = 0
T_TODAY = 0

Do While NOT RS.EOF %>
<%
td_dt = now()

if datediff("d",RS("last_dt"), td_dt) <> 0 then  'check to see if the hits are todays
    todays_hits = 0   'hits not today so dont include
else
   todays_hits = RS("today")  'they were today so include
end if
if datediff("M",RS("last_dt"), td_dt) <> 0 then ' check to see if the hits are this months
   MONTHS_hits = 0
else
   MONTHS_hits = RS("MONTH")
end if
%>
<tr>
<td width="80%">
<a href="<%= RS("page_name") %>"><%= RS("page_name") %></a></font>
</td>
<td align="right">
<font size="2">
<%= RS("total") %><% total = total + CLng(RS("total")) %>
</td>
<td align="right">
<font size="2">            'print and add to running totals
<b><%= todays_hits %><% T_TODAY = T_TODAY + CLng(TODAYS_HITS) %></b>
</td>
<td align="right">
<font size="2">
<b><%= MONTHS_hits %><% T_MONTH = T_MONTH + CLng(MONTHS_hits) %></b>
</td>
</tr>

<% RS.MoveNext
Loop

RS.Close
Conn.Close

set rs = nothing
set conn = nothing %>


</table><BR><BR><CENTER>

'write out grand totals
<BOLD>TOTAL PAGE HITS: <%=total%><BR>
TOTAL HITS TODAY:<%=T_TODAY%><BR>
TOTAL THIS MONTH:<%=T_MONTH%><BR> <br>


CURRENT ONLINE USERS : <%=APPLICATION("a_online_user")%> <BR>
HIGHEST NUMBER OF SIMULTANEOUS CONNECTIONS: <%=APPLICATION("a_max_connect")%>

The following code will need to added to your global.asa file:

<SCRIPT LANGUAGE = "VBScript" RUNAT = "SERVER">

Sub application_OnStart
  Application("a_online_user") = 0    'init our 2 variables
  Application("a_max_connect") = 0
End Sub

Sub Session_OnStart
  Application.Lock   'Lock the application
  online = CInt(Application("a_online_user")
  online = online + 1
   Application("a_online_user") = online   'increment current online users

   max_online = CInt(Application("a_max_connect")
   if online > max_online then   'see if the # of users online is at the highest point
       Application("a_max_connect") = online   'update highest number
   end if
   Application.Unlock
End Sub

Sub Session_OnEnd
   Application.Lock    ' User left, subtract 1 from online users
   online = CInt("Application("a_online_user")
   online = online - 1
   Application("a_online_user") = online
   Application.UnLock
End Sub

THE WORKING EXAMPLE

Share:
Home
Mobile Site | Full Site
Copyright 2017 © QuinStreet Inc. All Rights Reserved