Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 26

Thread: Visitor Counter

  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Posts
    317
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Visitor Counter

    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!!

  • #2
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post

    Smile

    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
    Omnis mico antequam dominus Spookster!

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Posts
    317
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thanks!

    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!!!

  • #4
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    Ok, no problem... I'll make it up after I finish with my website stats app..

    Shouldn't be to long.
    Omnis mico antequam dominus Spookster!

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Posts
    317
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Just do it in normal ASP format, afraid in flash too complicated...
    waiting for ur good news..

  • #6
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #7
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    Sorry I haven't had time to make you up one.
    Also I didn't mean in flash. lol I mean really quickly..

    Code:
    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:
    Code:
    This page has been accessed <%=NewHits%> times. You are visitor <%=NewUniques%>.
    Omnis mico antequam dominus Spookster!

  • #8
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

    Code:
    
    <%
    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"
    %>
    
    Last edited by whammy; 01-25-2003 at 01:20 AM.
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #9
    Regular Coder
    Join Date
    Jun 2002
    Posts
    317
    Thanks
    0
    Thanked 0 Times in 0 Posts

    thanks!

    Thanks so much MHTML

  • #10
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    No problem charon.

    Duh... what a brilliant idea. I'm going to update my text counter to do unique and total hits, lol.
    lol
    Omnis mico antequam dominus Spookster!

  • #11
    Regular Coder
    Join Date
    Jun 2002
    Posts
    317
    Thanks
    0
    Thanked 0 Times in 0 Posts

    problem understand ur code!

    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!!

  • #12
    Regular Coder
    Join Date
    Jun 2002
    Posts
    317
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Application Object!

    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!!

  • #13
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #14
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    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.
    Code:
    
    <%
    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.
    Omnis mico antequam dominus Spookster!

  • #15
    Regular Coder
    Join Date
    Jun 2002
    Posts
    317
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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...


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •