First off, we need to create the functions which will read and save the counter (increase counter before if new visitor). Copy the following code into a new text file and save the file as counter.aspx:
[code="JScript"]
<%@ Page Language="JScript"%> <%@ Import Namespace="System.IO" %>
<% var FilePath:String = Server.MapPath("\\") + "counter.txt";
function getCounter():String { var SR:StreamReader = File.OpenText(FilePath,FileMode.Open); var Counter:String = SR.ReadLine().ToString(); SR.Close();
var Cookie:HttpCookie; Cookie = Request.Cookies("OldVisitor");
if(Cookie==null) { var CounterInt:int = Convert.ToInt32(Counter); CounterInt++; Counter = Convert.ToString(CounterInt);
var FS:FileStream = new FileStream(FilePath, FileMode.Open, FileAccess.Write); var Text:StreamWriter = new StreamWriter(FS); Text.WriteLine(Counter); Text.Close(); FS.Close();
Cookie = new HttpCookie("OldVisitor","true"); Cookie.Expires = DateTime.Now.AddSeconds(120); Response.AppendCookie(Cookie); }
return Counter; }
Response.Write(getCounter()); %> End Sub |
[/code]
You should now place the counter.aspx in your root folder (i.e. c:\InetPub\wwwroot) on your Internet Information Server. Also, create a file called counter.txt and set the read/write security options to full access for the user your are running under IIS. Open the counter.txt file, write a number (i.e. 999) and save the file.
When you now open the URL http://localhost/counter.aspx in your browser, you will see a text counter. The counter has already add one count to the number you put in counter.txt.