Active Server Pages Programming (ASP)/web counter
Expert: Hari Priya - 10/20/2009
QuestionQUESTION: Hi experts,
I am about to start creating a web stats on my page using javascript but when i heard that counter should be done on the server I dont know how to start. I have knowledge in classic asp but I dont have any idea how to start this. Can you please let me know? Thnks..
ANSWER: Hi lems,
First you should create a text file called hit_count.txt with an initial value 0 in it and upload to the server, then using the file stream object, you should increment the value of hit_count.txt for each time your index.asp page is clicked. You can display the number of counts by reading the text file.
I am giving you a sample working code, if you have any issues with that, feel free to ask me.
<%
// This code will increment the txt file by the value 1 each time a visitor clicks your home page. Include this code in your home page (index.asp) //
Dim fsoObject
Dim tsObject
Dim filObject
Dim VNum
Dim LoopInc
Set fsoObject=Server.CreateObject("Scripting.FileSystemObject")
Set filObject=fsoObject.Getfile(server.Mappath("hit_count.txt"))
Set tsObject=filObject.OpenasTextstream
VNum=Clng(tsObject.ReadAll)
VNum=VNum+1
Set tsObject=fsoObject.CreateTextfile(Server.Mappath("hit_count.txt"))
tsObject.write Cstr(VNum)
Set fsoObject=Nothing
Set tsObject=Nothing
Set filObject=Nothing
%>
<%
// This part is to display the hit count //
// Copy and paste this code in your asp page, where you want to show the hit count //
Dim fsoObject
Dim tsObject
Dim filObject
Dim vnum
Dim LoopInc
Set fsoObject=Server.CreateObject("Scripting.FileSystemObject")
Set filObject=fsoObject.Getfile(server.Mappath("hit_count.txt"))
Set tsObject=filObject.OpenasTextstream
vnum =Clng(tsObject.ReadAll)
Set fsoObject=Nothing
Set tsObject=Nothing
Set filObject=Nothing
vnum = string(6 - Len(vnum), "0") & vnum
Response.write "Visitor Number:"
Response.write(vnum)
%>
---------- FOLLOW-UP ----------
QUESTION: Hi Hari Priya,
Thank you so much for your answer. I have now the idea of how the counter works. But I still have another problem since I want to put individual hits to every profile page. What I want to display is the counter stats of every profile in a page.
Does this possible using the File System Object were in this case I only have 1 page in common?
I knew I need a query string for this to work but I am new to this hit counter.Do I need to create a database for this to store the value instead of storing it in a textfile?
Please let me know.
Thank you so much Hari Priya.
AnswerHi lems,
I am not clear about what you are asking.
If you want to store the hits for each profile page, you can have different text files to store the stats, for example, hit_count1.txt, hit_count2.txt etc. Using the file object you can open any number of text files, consecutively and in any page.
Alternatively you can store the hit counter values in the database also. Create one table with as many fields as your number of profile pages.
Hope this helps.
Regards,
Priya