...

View Full Version : Visitor Counter



charon
01-23-2003, 03:04 AM
hi,
I'm asking a very simple answer (damn bad that I don't know)...
How do I create the statement for counting the Total Visitors, like what we always seen at most of the web-sites: You Are Visitor Number XXX

Please advice!!

Mhtml
01-23-2003, 04:37 AM
Well you need to decide which format you wish to store the counts in. This can be in a text file or a database.

Now, a text file is good but it doesn't allow you to easily differentiate between a new visitor (unique) and a visitor returning.

Now if you just want to count the hits then you can use a text file. A hit means that everytime the page is loaded +1 is added to the count even though the person loading it many not be unique.

But if you wish to count unique hits, that is a true counter then you should go for a database.

So if you tell me which way you wish to go then I can whip you up an example in a flash. :) I suggest a database

charon
01-23-2003, 04:44 AM
hi ,

Thanks si much...
Can I have two format, means that I want to know the "Hits" without knowing whether he/she is a returning visitor, and also the Unique "Hits". Both important for my site analysis.

Wuh!!!

Mhtml
01-23-2003, 04:55 AM
Ok, no problem... I'll make it up after I finish with my website stats app..

Shouldn't be to long. :)

charon
01-23-2003, 08:47 AM
Just do it in normal ASP format, afraid in flash too complicated...
waiting for ur good news..

raf
01-23-2003, 01:32 PM
why not use the build in page counter component ?
(combined with reading cookie).


more info from helpfile:
---------------------------------
Page Counter Component
The Page Counter component creates a PageCounter object that counts and displays the number of times a Web page has been opened. At regular intervals the object writes the number of hits to a text file so that in the event of a server shutdown, the data is not lost. The Page Counter component uses an internal Central Management object to record how many times each page in the application has been opened.

When an instance of the PageCounter object is created on a page by using the Server.CreateObject method, the object retrieves the current hit count for the specified Web page from the Central Management object. The object can then be manipulated with the methods it exposes.

The Page Counter component uses the following files:

File Names
pagecnt.dll The Page Counter component.
Hit Count Data file The text file to which the Page Counter component saves the current hit count.


Syntax
<% Set oVar = Server.CreateObject("MSWC.PageCounter") %>

Parameters
oVar
Specifies the name of the PageCounter object created by the call to Server.CreateObject.
Registry Entries
The Page Counter adds the key MSWC.PageCounter to the registry when the object is compiled or registered. The key is added under HKEY_CLASSES_ROOT and contains the following named values.

Named Value Description
File_Location A string that specifies the path and filename of the Hit Count Data file. The default filename is hitcnt.cnt. This file is located in your Windows directory.
Save_Count A DWORD that specifies the number of hits before the hit count is saved to the Hit Count Data file. Note that this is the total number of hits, not the number of hits per page. The default value is 25.


Methods
Hits Displays the number of times that a specified URL has been opened.
PageHit Increments the Hit Count.
Reset Sets the hit count for a specified page to 0.


Example
The following example uses the PageCounter object to track the number of visitors to the page and sends a special message to the millionth visitor.

<%
Set MyPageCounter = Server.CreateObject("MSWC.PageCounter")
HitMe = MyPageCounter.Hits

If HitMe = 1000000 Then
%>
You are the lucky 1,000,000th Customer!!! <BR>
<% Else %>
Sorry, you are customer #<%= HitMe %> <BR>
<% End If %>

Note This component is not installed with IIS. You can install it from the Microsoft Web site at: http://www.microsoft.com/iis/, or from the CD included with the IIS Resource Kit.

Mhtml
01-23-2003, 04:37 PM
Sorry I haven't had time to make you up one.:(
Also I didn't mean in flash. lol I mean really quickly..



Set conn = server.createObject("Adodb.connection")
Set rs = Server.createObject("Adodb.recordset")
conn.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ="&server.mapPath("db/counter.mdb")&";"

sqlGetCount = "SELECT * FROM Counter"
rs.Open sqlGetCount, conn

CurrentUnique = rs("Unique")
CurrentHits = rs("Hits")
NewUnique = CurrentUnique + 1
NewHits = CurrentHits + 1

If Request.cookies("Counted") = "true" Then
sqlUpdateCounter = 'UPDATE Counter SET Hits = " & NewHits
Else
sqlUpdateCounter = "UPDATE Counter SET Unique = " & NewUnique & " , Hits = " & CurrentHits
Response.cookies("Counted")
End If

rs.Close
rs.Open sqlUpdateCounter, conn
Set rs = Nothing
conn.Close
Set conn = Nothing


Now to print the hits and or the uniques count just insert <%=NewHits%> for the hits and <%=NewUniques%> for the uniques.

Example:


This page has been accessed <%=NewHits%> times. You are visitor <%=NewUniques%>.

whammy
01-25-2003, 12:36 AM
Duh... what a brilliant idea. I'm going to update my text counter to do unique and total hits, lol. ;)

P.S. for a very simple counter, check out this FileSystemObject script (I just shortened it a bit):

http://www.solidscripts.com/displayscript.asp?sid=4



<%
Dim cfs, cf, thefile, counter
sMapPath = Server.MapPath("\")
thefile = Mid(sMapPath, 1, InStrRev(sMapPath,"\")-1) & "\database\counter.txt"
Set cfs = CreateObject("Scripting.FileSystemObject")
If cfs.FileExists(thefile) Then
Set cf = cfs.OpenTextFile(thefile, 1)
counter = cf.ReadLine
End If
Set cf = cfs.OpenTextFile(thefile, 2, True)
If Session("counted") <> "1" Then counter = counter + 1
cf.WriteLine(counter)
Set cf = Nothing
Set cfs = Nothing
Session("counted") = "1"
%>

charon
01-25-2003, 03:33 AM
Thanks so much MHTML

Mhtml
01-25-2003, 03:37 AM
No problem charon. :)


Duh... what a brilliant idea. I'm going to update my text counter to do unique and total hits, lol. ;)

lol :)

charon
01-25-2003, 04:33 AM
hi MHTML,

Alamak, I have problem on understanding your code:

1.) Where should I put the code
2.) Is the Request.cookies("Counted") property/atribute if cookies??

If Request.cookies("Counted") = "true" Then
sqlUpdateCounter = 'UPDATE Counter SET Hits = " & NewHits
Else
sqlUpdateCounter = "UPDATE Counter SET Unique = " & NewUnique & " , Hits = " & CurrentHits
Response.cookies("Counted")
End If

3.) How about Whammy code for the session("counted")??

Please advice!!

charon
01-25-2003, 05:57 AM
hi, WHTML,

Why you din't mention about using the Application Object for counting the Unique user which listed as below:

<SCRIPT LANGUAGE=VBScript RUNAT=Server>

Sub Application_Onstart()

'Set the variable to 0 when the application starts
Application("Counter") = 0

End Sub

Sub Session_OnStart()

'Lock the application so we don't get the wrong numbers
'then add one to the application variable "hits" and
'unlock the application again
Application.Lock
Application("Counter") = Application("Counter") + 1
Application.Unlock

End Sub

</SCRIPT>

1.) from my understanding, the session object can't associated with individuals, I mean the Counter will be increaced once there has user fetchs the page, if she/he open the new browser, the counter will be increased 1, it won't bother whether she/he is a returning visitor...How do I make it exactly "Unique"?????

Please advice!

p/s: Do reply my revious question as well..thanks so much!!

whammy
01-26-2003, 01:08 AM
You can't TRULY count unique hits, unless you have every visitor to your website register and login.

Other than that the best you can do is probably IP address, although those change.

You might want to do a search of the forums, quite a few working scripts have been posted here that do all of the above, even using an Access database, etc.

Mhtml
01-26-2003, 04:01 AM
Using the application variable is the worst idea possible.
If the server goes down, bang your count is gone.

The request.cookies("Counted") just requests the users cookies, more specifically the one called counted.
Now if they have already been counted the value of that cookie will be "true" else it will be nothing.

Whammy's "session code" just makes to put it simple a temporary cookie. As soon as you leave that website the session is dead and burried.

As for placing the code just put it at the very top of your page encased in the appropriate delimiters "<%" and "%>" so the code will look like.


<%
Set conn = server.createObject("Adodb.connection")
Set rs = Server.createObject("Adodb.recordset")
conn.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ="&server.mapPath("db/counter.mdb")&";"

sqlGetCount = "SELECT * FROM Counter"
rs.Open sqlGetCount, conn

CurrentUnique = rs("Unique")
CurrentHits = rs("Hits")
NewUnique = CurrentUnique + 1
NewHits = CurrentHits + 1

If Request.cookies("Counted") = "true" Then
sqlUpdateCounter = 'UPDATE Counter SET Hits = " & NewHits
Else
sqlUpdateCounter = "UPDATE Counter SET Unique = " & NewUnique & " , Hits = " & CurrentHits
Response.cookies("Counted")
End If

rs.Close
rs.Open sqlUpdateCounter, conn
Set rs = Nothing
conn.Close
Set conn = Nothing

%>


Also, I trust that you can create an access database containing a table and the fields which are used in the code above.

charon
01-27-2003, 03:04 AM
hi, MHTML,

The hits is the page hits right, just assume that we 200 pages and the visitor view each page once, then we will have 200 hits..correct???

Thanks so much for the reply...

Mhtml
01-27-2003, 03:09 AM
Well, yes.

That is disturbing. I forgot a session variable in the code to stop the visitor from being counted everytime the page loads.




<%
SELECT CASE Session("BeenHere")
CASE "True"
Session("BeenHere") = "True"
Set conn = server.createObject("Adodb.connection")
Set rs = Server.createObject("Adodb.recordset")
conn.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ="&server.mapPath("db/counter.mdb")&";"

sqlGetCount = "SELECT * FROM Counter"
rs.Open sqlGetCount, conn

CurrentUnique = rs("Unique")
CurrentHits = rs("Hits")
NewUnique = CurrentUnique + 1
NewHits = CurrentHits + 1

If Request.cookies("Counted") = "true" Then
sqlUpdateCounter = 'UPDATE Counter SET Hits = " & NewHits
Else
sqlUpdateCounter = "UPDATE Counter SET Unique = " & NewUnique & " , Hits = " & CurrentHits
Response.cookies("Counted")
End If

rs.Close
rs.Open sqlUpdateCounter, conn
Set rs = Nothing
conn.Close
Set conn = Nothing
Case Else
'Do nothing...
End Select
%>



The above ammended code will make it so that a visitor will only be counted 1 per session as a hit.

charon
01-27-2003, 03:32 AM
hi, WHTML,

Again, I'm a bit confused on the "Variable" that you are using. Are these two "Variables" properties for the Session and Cookies.
Session("BeenHere")??
---------------------------------------------------------------------------------
The request.cookies("Counted")
------------------------------------------------------------------------------

Does it mean I can't change it to any name such as Session("Visit") and Request.Cookies("VisitCounted")??

Why I never see them in all of the reference that I have come acrossed??

Thanks again!

Mhtml
01-27-2003, 04:13 AM
Cookies - www.w3schools.com/asp
Good reference on cookies.

Anyways back to the subject. :)

The cookie that I request in the code is used in a conditional statement. Meaning that if the cookie called Counted is equal to true then something will happen.

If it isn't then something else will happen.

The session variable works in the exact same way, except it is only alive while the person is on your website, as soon as they leave it dies and therefore the value is non-existant. S the point of this session variable is to stop the user from being counted while they are at your website. They will be counted as they enter but while they stay they won't be counted again.

In summary:

The user loads your website for the first time, they are counted as a hit and a unique.
Then they are planted with a cookie which identifies that they have been here before, hence "counted".
Next a session variable is applied, "BeenHere" now that stops the visitor from being counted again while he/she browsers your site.

The next day the user returns, they are counted as a hit and the session is re-applied and the user is not counted while they are there.


Does it mean I can't change it to any name such as Session("Visit") and Request.Cookies("VisitCounted")??

Why I never see them in all of the reference that I have come acrossed??


Ok, now you can change the name of the variables that you are requesting. But you will also need to change the name of the variable when it is assigned.

So if you were to change the cookie to Request.Cookies("VisitCounted") you will also need to change the Response.Cookies("Counted") to ("VisitCounted") .

Same goes with the session.

charon
01-27-2003, 04:50 AM
Thanks WHTML for explaning so much...
Actually i will understand it if I really go through your code again and again....
Now I 100% understand alreday.......I try to explain in my way (I'm chinese educated, not so good in english, so sometime might get confuse with those explaination).

If the user first time access, Response.Cookies("Counted") will be created, later when he/she return, will compare the Cookies("Counted") to see whether it has existed, it YES, then it is returning user. Am I Right???

If the user browser set teh cookies disable then will not be counted....

Mhtml
01-27-2003, 08:01 AM
You are correct.
Except that they will be counted if cookies is disabled.

Possibly you could add.


Response.Cookies("color") = "Purple"
If Response.cookies("color") = "Purple" Then
Response.redirect("BadCookie.htm")
End If


And on the BadCookie.htm page you could have the following to double check if cookies is enabled.



<script language="javascript">
if (navigator.cookieEnabled != true) {
document.write("You do not have cookies enabled!")
}else{
document.write("Your browser does not support javascript or it is turned off, please turn it on.")
}
</script>


That will double check and give a report to the user.
This is unlikely to occur though.

charon
01-27-2003, 09:16 AM
ok..temporary end our Discussion here...will come back if i have any question on this. Thanks so much:))

Mhtml
01-27-2003, 10:20 AM
No problems. :)

Vladdy
01-31-2003, 09:30 PM
And what if a visitor has cookies disabled??? Then his every hit will be counted as unique!!! Forseeing possible answers: you can not use IP address either, since if there is a company with a few thousand people behind a router (which IP you will get) you end up counting all of them as one.
I yet to see a good solution to this problem.... wish I could get my hands on MAC address HTTP request originated from...

charon
04-23-2003, 08:42 AM
Back again, Session and Cookies questions:
1.) In Order to get the Unique hits and Page hits we must use cookies (response.cookies) instead of session (only alive when we are active in the site)??
2.) Do we need to put the code on top of every web pages for our web-sites for getting the Unique Hit and page hits??
3.) what is the common cookies variables (Response.Cookies("Counted") = True, if we didn't required username) that we will be used?? Would it be a problem if site A and Site B have same cookies variable -> Response.Cookies("Counted"), if yes, would it be confused???
4.) When the user close the window the session will be end right?? (it is true when i tried myself)
5.) I try to explain how does the session works, correct me if I'm wrong???
As we know that a sessionID will be generated once we request any asp page from a site. Th sessionID then will be saved in user machine as cookies with the sessionID. In order to associate with the individual, When the user access to other page from the asp page , the server first will check the sessionID from
its listed and compare with the sessionID at the user machine. If true then it considers she/he is the same user until session end.
6.) When u access to the amazon.com without login and choose the books we intend to buy, add to cart, after that close the window. When you come again later on, you will find that it be able to trace our last transsction by showing you your preview's cart.
I wonder what approach they are using??
Are these the way:

1.) When the user request a page it will generate a sessionID, the sessionID then will be saved into file/database by amazon.com for next reference . Another copy of the sessionID will be saved in the user's pc by generating Response.Cookies("key")("value") = "amazon.com", Session.SessionID as well as others cart details such as :
Response.Cookies("key")("item") = "amazon.com", itemName
Response.Cookies("key")("itemcount") = "amazon.com", quantiti else how they associated me with my transaction details???

whammy
04-24-2003, 01:20 AM
1. ASP sessions depend upon cookies being enabled.

If you don't want "repeat hits" over MULTIPLE sessions to be counted etc., you would check a cookie, yes.

But usually it's helpful to know if you have repeat visitors, these are people who find your site interesting enough to visit more than once! I'm not sure I quite understand the gist of this question, but it seems you're on the right track.

Cookies reside on the user's computer, whereas Sessions are stored in temporary memory on the server, but require that cookies are enabled on the client machine in classic ASP.

2. Yes - but an include file will work just as well...

3. Cookies are unique to a domain - so as long as the domain is different it won't cause a problem. If there IS a problem with that, put some logic in to figure out what page you're on (look at the request.servervariables sticky post!).

4. Not necessarily - usually when the user closes their browser, the default Session.Timeout kicks in (20 minutes unless you specify it). I have heard of (and seen) some issues with this not working correctly in ASP.

5. I haven't messed with the SessionID too much, but from what I understand it's NOT guaranteed to be totally unique. I would probably use a server-side timestamp combined with some other criteria (or just an autonumber field perhaps) to make sure I have a unique value.

6. Sounds plausible - try it out!

charon
07-31-2003, 01:58 PM
Hi, Mhtml,

I'm back after some time......



<%
SELECT CASE Session("BeenHere")
CASE "True"
Session("BeenHere") = "True"
Set conn = server.createObject("Adodb.connection")
Set rs = Server.createObject("Adodb.recordset")
conn.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ="&server.mapPath("db/counter.mdb")&";"

sqlGetCount = "SELECT * FROM Counter"
rs.Open sqlGetCount, conn

CurrentUnique = rs("Unique")
CurrentHits = rs("Hits")
NewUnique = CurrentUnique + 1
NewHits = CurrentHits + 1

If Request.cookies("Counted") = "true" Then
sqlUpdateCounter = 'UPDATE Counter SET Hits = " & NewHits
Else
sqlUpdateCounter = "UPDATE Counter SET Unique = " & NewUnique & " , Hits = " & CurrentHits
Response.cookies("Counted")
End If

rs.Close
rs.Open sqlUpdateCounter, conn
Set rs = Nothing
conn.Close
Set conn = Nothing
Case Else
'Do nothing...
End Select
%>



1.) I confuse with your code above.....for me if you set the Session("BeenHere") = "True" after the Select Case "True", then the Session("BeenHere") = "True" will be always the same, The above ammended code will NOT make it so that a visitor will only be counted 1 per session as a hit. It will count when the user reload the page again.......

2.) For the second part :


If Request.cookies("Counted") = "true" Then
sqlUpdateCounter = 'UPDATE Counter SET Hits = " & NewHits
Else
sqlUpdateCounter = "UPDATE Counter SET Unique = " & NewUnique & " , Hits = " & CurrentHits
Response.cookies("Counted")
End If


The Last two line -> Response.cookies("Counted") should be Request.cookies("Counted") = "true", else how could it check the condition If Request.cookies("Counted") = "true" Then
sqlUpdateCounter = 'UPDATE Counter SET Hits = " & NewHits.

Please advice......



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum