Solved

asp.net 3.5 hit counter

Posted on 2011-03-20
4
570 Views
Last Modified: 2012-06-21
Hi experts

Using asp.net 3.5 with vb.net as the code-behind, has anyone got any examples of a hit counter, I need the counter to increment for each visitor to the site and not every time the page is loaded, hope somebody can help.
0
Comment
Question by:dizzycat
  • 2
4 Comments
 
LVL 6

Expert Comment

by:AkAlan
Comment Utility
When users hit your site, do they have to log in or do you need a counter for the interface page?
0
 

Author Comment

by:dizzycat
Comment Utility
I would just like a counter for the interface page, there is no log-in on my site.
0
 
LVL 6

Accepted Solution

by:
AkAlan earned 500 total points
Comment Utility
Here is an example I found here:
http://www.daniweb.com/web-development/aspnet/threads/5145


PrivateSub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load

IfNot Page.IsPostBack Then
Dim nCount As Int32 = 0
nCount = GetCounterValue()
lblTest.Text = nCount.ToString()
EndIf
EndSub

PrivateFunction GetCounterValue() As Int32

Dim ctrFile As StreamReader
Dim ctrFileW As FileStream
Dim sw As StreamWriter
Dim strPath AsString = Server.MapPath("indexcount.txt")
Dim strCounterContents AsString
Dim nCounter As Int32
 
If (File.Exists(strPath)) Then
ctrFile = File.OpenText(strPath)
strCounterContents = ctrFile.ReadLine().ToString()
ctrFile.Close()
nCounter = Convert.ToInt32(strCounterContents)
Else
nCounter = 0
EndIf
nCounter += 1
ctrFileW = New FileStream(strPath, FileMode.OpenOrCreate, FileAccess.Write)
sw = New StreamWriter(ctrFileW)
sw.WriteLine(Convert.ToString(nCounter))
sw.Close()
ctrFileW.Close()
Return nCounter
EndFunction

  
PrivateSub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load

IfNot Page.IsPostBack Then
Dim nCount As Int32 = 0
nCount = GetCounterValue()
lblTest.Text = nCount.ToString()
EndIf
EndSub

PrivateFunction GetCounterValue() As Int32

Dim ctrFile As StreamReader
Dim ctrFileW As FileStream
Dim sw As StreamWriter
Dim strPath AsString = Server.MapPath("indexcount.txt")
Dim strCounterContents AsString
Dim nCounter As Int32
 
If (File.Exists(strPath)) Then
ctrFile = File.OpenText(strPath)
strCounterContents = ctrFile.ReadLine().ToString()
ctrFile.Close()
nCounter = Convert.ToInt32(strCounterContents)
Else
nCounter = 0
EndIf
nCounter += 1
ctrFileW = New FileStream(strPath, FileMode.OpenOrCreate, FileAccess.Write)
sw = New StreamWriter(ctrFileW)
sw.WriteLine(Convert.ToString(nCounter))
sw.Close()
ctrFileW.Close()
Return nCounter
EndFunction

Open in new window


0
 
LVL 8

Expert Comment

by:Anurag Agarwal
Comment Utility
buddy just go for http://www.hit-counts.com/ ( such types of links, get you site link register it, and get visitor no automatically calculated by this site. u wiil get a link  paste the javascript wherever u want to print the visitor no like eg


 <tr>
 <td align ="center">Visitor No.      </td>
 <td align ="left" style="text-align: left">
  <img src='http://www.hit-counts.com/counter.php?t=10&digits=9&ic=72000&cid=74644' border='0' alt=''>
</td>
</tr>



the image source u will get after registration (underlined ) its free and easy to manage, with various designs available.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Envision that you are chipping away at another e-business site with a team of pundit developers and designers. Everything seems, by all accounts, to be going easily.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
This video teaches users how to migrate an existing Wordpress website to a new domain.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now