E-Business | Main Page | My Profile | Home
HTTP is a stateless protocol. This means a HTTP server needs not keep track of any state information. So,
At any time, client can send any valid command. The server will not relate this command to any previous or future commands. Each command is independent of any other commands, past or future. Compare this to a stateful protocol like POP3: after you enter a USER command you must enter a PASS command.
A HTTP server wil not remember whether a client has visited it before, or how many time.
Thus, a HTTP server is not required to remember anything. But if this is the case, then how come you can order from amazon.com by simply telling it your name and a password; it then knows to charge your visa card that you told them a year ago?
There are many clever schemes that make web server appears stateful. Of course, the server continues to function in the old stateless way.
First a disclaimer: I do not encourage you to use hit counters.
Clearly, to have a persistent counter, we must use a file to record the hit count. If you can read the access_log of the server, then you can simply read the access_log, and count the number of lines that contain the URI of the page. This will be the hit count of the page. This can be achieved by piping the output of a 'grep' to 'wc -l'. In this case, you do not need to create a new file.
If however, you do not have read access to access_log, then you can create a file, say, count.txt that contains the current count.
Any time the page is requested, the server needs to run a program
to read the file (so the file must be world readbable)
increment the count by 1, and then
write the count back to the file (so, the file must be world writable too).
A simple way of implementing the above idea is to use the Server-Side Includes.
A Server-side includes file is identified by the suffix .shtml. When asked to retrieve a shtml, the server does not just return the file, as it does in the case of html files. Instead, it will parse the shtml file to look for specially formatted html comments embeded in the text. These commands take the form of
<!--#directive param1="value1" param2="value2" ... -->
Follow this simple procedure:
<!--#exec cmd="echo $[`cat count.txt` + 1] | tee count.txt" -->
In this example, the server does nothing special until it reaches the embeded command in 3. At that point, it executes a shell command, and inserts the output from the shell command in-place.
Back 2 Top