Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 02-24-2003, 12:42 PM   #1 (permalink)
Sarlok
Registered User
 
Join Date: Feb 2003
Location: Nowhere special
Posts: 10
Sarlok is on a distinguished road
Send a message via ICQ to Sarlok
Hit Counter

Don't you just hate when that happens? Halfway through writing the tutorial, Internet Explorer crashes. I'm back in beloved Gentoo now, so we'll try this again, shall we?

Here's the code we'll be working on for our counter:

Code:
<%
Set FS=Server.CreateObject("Scripting.FileSystemObject")
Set RS=FS.OpenTextFile(Server.MapPath("counter.txt"), 1, False)
  counter=RS.ReadLine
RS.Close
counter=counter+1
Set RS=FS.OpenTextFile(Server.MapPath("counter.txt"), 2, False)
  RS.Write counter
RS.Close

Set RS=Nothing
Set FS=Nothing
%>
Okay, let's get started. The <% and %> just mark the beginning, and end of our code.

Set FS=Server.CreateObject("Scripting.FileSystemObject ")
This line creates a "File System Object" using FS. But, for the sake of simplicity, it lets us access files. If you want, you can change FS to something else, I just use it because it's simple and easy to rememer (FS - FileSystemobject).

Set RS=FS.OpenTextFile(Server.MapPath("counter.txt"), 1, False)
This sets things up so we can open our text file that will keep track of how many hits we have. If you decide to change FS like I mentioned you could before, do the same here, as well as anywhere else it's mentioned further on.
Again, you can change RS here to whatever you want, it just makes the next few parts of code smaller, and easier to edit.
The 1 on the end of this line means "Open the file, just so we can read from it". 2 would mean "Open the file so we can read from it, and write things to it". but we only want to read from it right now, so it's good to use 1.

counter=RS.ReadLine
Now, this line reads the number from the counter text file, and stores it in a value "counter". We can use this later on to display how many hits your page has had.

RS.Close
Now, we close the file, we're done reading from it.

counter=counter+1
Now, this code will run every time somone visits your website. So, assuming it's running now and has come this far, we can pretend somone has just visit your website. So, we want to add a hit to your counter. This line does that, by adding 1 to the number we stored in "counter".

Set RS=FS.OpenTextFile(Server.MapPath("counter.txt"), 2, False)
Here we open our counter text file again, but this time we open it to write to, so we can save the new hit.

RS.Write counter
Write the new number in counter to the text file.

RS.Close
Close the file again, we're done!

Set RS=Nothing
Set FS=Nothing
These two lines are sort of good practice, but not really needed. They just make FS and RS useless unless we set them again with those big lines at the top.

Now, wasn't that fun? Don't celebrate yet, we need to put the counter in out page! Here's how we do that:
Code:
<html>
<body>

This page has been visited <%=counter%> times.

</body>
</html>
That's it. Just add =counter to wherever you want the number of hits to your page displayed. The <% and %> are there again, because we have to let the server know that there's some code in the middle of that webpage.

Last notes:
I would advise putting the counter code at the very top of the page you want a counter on, that way it will update your hits before te number of hits is displayed. See below for a full example.
In order for this to work, you will need write permissions for scripts on your website. You can check with your websites host if you aren't sure if you do or not.
I also suggest creating the counter.txt (or whatever you decide to call it) and then giving it a number to begin with... say, 0, then upload that.

Now you can celebrate.

Code:
<%
Set FS=Server.CreateObject("Scripting.FileSystemObject")
Set RS=FS.OpenTextFile(Server.MapPath("counter.txt"), 1, False)
  counter=RS.ReadLine
RS.Close
counter=counter+1
Set RS=FS.OpenTextFile(Server.MapPath("counter.txt"), 2, False)
  RS.Write counter
RS.Close
Set RS=Nothing
Set FS=Nothing
%>
<html>
<body>

This page has been visited <%=counter%> times.

</body>
</html>
Sarlok is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Counter nightcrawler MS Technologies ( ASP, VB, C#, .NET ) 1 10-04-2004 05:12 AM
Firefox browser to hit 1.0 milestone sde Code Newbie News 0 09-14-2004 10:11 AM
Hackers hit supercomputing giants sde Code Newbie News 0 04-17-2004 05:00 PM
Counter Strike Ilya020 Lounge 12 04-26-2003 10:22 AM
Counter Strike server Ilya020 Lounge 27 03-10-2003 05:10 PM


All times are GMT -8. The time now is 11:39 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting