Introduction
Hit counter is web based technique to indicate the number visitor visited your
website or hits your website or even you can say it counts how many times
particular page viewed. Hit counter displays the visits in numerical form. Once
set up, these counters will be incremented by one every time the web page is
accessed in using any browser. There are lots of techniques to create hit
counters for web application. But most the JavaScript based hit counters fails
on most browsers.
Perquisite
Before getting started to create Hit Counter, you need the following things:
(i) A Notepad file in root directory like counter.txt
(ii) Notepad file have read, write and all access permission granted
(iii) A ASP.Net page like counter.aspx
(iv) A ASP.Net page like Default.aspx which will open the counter.aspx page
Working Technique
A Notepad file should be exist on root directory having all permissions granted
like read, write and access from every angles, as you can say it has not any
deny. In Notepad file we only have to write 1 (one) not any other character.
Codes for counter.aspx Page
<%@ Import Namespace="System.IO" %>
<%@ Page Language="VB"%>
<html>
<head>
<title>Hit
Counter</title>
<script language=VB runat="server" id="count">
Sub
Page_Load(Src as object,
E as EventArgs)
dim
objReader as StreamReader
dim
objWriter as StreamWriter
dim
sFile as string
dim
sCount as string
dim
iCount as integer
Try
sFile =
Server.MapPath(".")
If Right(sFile, 1) <>
"\" Then sFile
= sFile & "\"
sFile = sFile & "counter.txt"
If not File.Exists(sFile)
then
objWriter = File.CreateText(sFile)
objWriter.Write("0")
objWriter.Close
End if
objReader =
File.OpenText(sFile)
sCount = objReader.ReadToEnd()
objReader.CloseiCount =
Cint(sCount)
iCount = iCount + 1
sCount = iCount.ToString
objWriter =
File.CreateText(sFile)
objWriter.Write(sCount)
objWriter.Close
Catch
Ex as Exception
Label1.width = New Unit(640)
sCount = "[There is some error as]: " &
Ex.Message
Finally
Label1.Text =
sCount
End Try
End Sub
</script>
</head>
<BODY>
<p>
<img src="abhi.JPG" style="font-size: 10pt;
width: 139px;
font-family: Verdana; height: 174px;" /> </p>
<p>
<span style="font-size: 10pt;
font-family: Verdana">
Thank you for visiting ....</span></p>
<p>
<a href="http://www.itorian.com">Blog
Page</a></p>
<p>
<span style="font-size: 10pt;
font-family: Verdana; color: #ff0033;"><strong>
You are our visitor number : </strong></span>
<asp:label id=Label1 runat="server" Height="21px" Width="57px" Font-Bold="True" Font-Size="X-Large" ForeColor="Blue"></asp:label></p>
<p>
</p>
</BODY></html>
Codes
for Default.aspx Page
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Hit
Counter</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<strong>Welcome
to counting world. </strong><br />
<br />
<br />
<input type="button" onclick="window.open('counter.aspx')" value="Open Hit Counter" style="width: 125px"/>
<br />
<br />
<br />
You can setup this Hit Counter in any even, procedure by calling
<span style="color: #0000ff">
window.open('counter.aspx')<span style="color: #000000">
page.</span></span></div>
</form>
</body>
</html>
Now run the project to count the total hits.
HAVE A HAPPY CODING !