Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

counter

Status
Not open for further replies.

lintow

Programmer
Nov 29, 2004
82
0
0
US
I would like to setup a counter on my website
I want to display the number of people that have accessed my website.

Is there a way to do this?

Thanks in advance
 
You could also write it yourself by checking to see if a session variable is set. Something like:
Code:
If Session("HitCounter") = "Yes" Then
  'Do nothing, already registered current visitor
Else
  'Insert record into HitCounter table
  Session("HitCounter") = "Yes"
End If
Then query the table with a count for # visitors. This way if they come back to a current page, they don't get listed as a returned user. Session variable will be valid for the length of time your server has this time set for, or you can set it in your page, or until they close the browser.
Drawback: a session variable is created for every user visiting your site. Session variables take up resources, so I am not sure how good of a thing it is for a website with lots of users.

Hope this helps.
 
The session would be destroyed after the user closes the browser. ANother quick solution is to drop a cookie and query it to prevent users from being counted more than once (although even in this case, if they flush all cookies on the system thay will be counted again).

QatQat

Life is what happens when you are making other plans.
 
Howdy QatQat,
I agree the session is destroyed after browser is closed. I feel justified in saying that if browser is closed and then user opens up the browser 10 minutes later and comes back to my website, that he is a new user.
Might be a good topic to post to see how people feel about this point.
 
Yup ksbigfoot,
I think if we post this one we will have some (many) different opinions but I think we are moving a bit into the philosophy of web.
Nevetheless, Lintow has to know that both methods work but both methods are cheap solutions; there is a lot of literature on the web about creating a better and more efficient hit counter.


CHeers

QatQat

Life is what happens when you are making other plans.
 
Howdy QatQat,
I was looking for some software I could buy for a hit counter. I couldn't find anything that I could load onto my server, so I decided to write my own.
Not the best, but it works for my clients.
Thanks
ksbigfoot
 
ok here's an idea...this uses an include file to record the data in an MS Access database.

First create a totalhits.asp page with this code:
Code:
<!--#INCLUDE VIRTUAL="/includes/connaction.asp" -->

<%
DIM mySQL, objRS
mySQL = "SELECT * FROM tblTotalHits" 
Set objRS = Server.CreateObject("ADODB.Recordset")
objRS.Open mySQL, objConn, adOpenKeyset, adLockPessimistic, adCmdText

DIM iRecordCount
objRS.MoveFirst
iRecordCount = objRS("Hits")
objRS("Hits") = iRecordCount + 1
objRS.Update
%>

Our website has received <% =objRS("Hits") %> total hits.

<%
objRS.Close
Set objRS = Nothing
objConn.Close
Set objConn = Nothing
%>

Then, add the totalhits.asp include file to every page in the site:

Code:
<!-- #INCLUDE VIRTUAL="/YOUR FILE PATH HERE/totalhits.asp" -->
<HTML><HEAD>
<TITLE>ASP Web Pro</TITLE>
</HEAD> 

<BODY>

The rest of your page here.

</BODY>
</HTML>

By the way: If you are using an HTML Editor like FrontPage or Dreamweaver, you can save time by placing your include file in your shared border or template. Then, your dailyhits.asp file will autmatically be included in every page that uses that shared border or template.

Now, you have a count of how many Total Hits your website has received.

Or the other way you could do it is using a text file like this...

Create a text file called totalhits.txt and save it in a directory called statistics.

Then, include the code below at the top of every page that you want hits counted from:

Code:
<%
Set FileObject = Server.CreateObject("Scripting.FileSystemObject")
HitsFile = Server.MapPath ("/statistics") & "\totalhits.txt"
Set InStream= FileObject.OpenTextFile (HitsFile, 1, false )
OldHits = Trim(InStream.ReadLine)
NewHits = OldHits + 1
Set OutStream= FileObject.CreateTextFile (HitsFile, True)
OutStream.WriteLine(NewHits)
%>

And you're done. A hit counter that uses a text file.

hope it helps.

-----
Best Regards
Rob Kelsey
_________________________________________________________________
My computer makes me say: "Somebody stop the world, I want to get off!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top