|
Tutorial 4 - Using a Cookie to Override Your Web Hit Counter
|
|
Installation:
|
|
Part I: Server side installation:
|
-
ftp article04.cgi to your cgi-bin/thecgibin directory
-
on telnet change directories to cgi-bin/thecgibin and
enter the command 'perl -wc article04.cgi'.
-
you should get the response 'article03.cgi syntax ok'
-
make the specified change to your count program, taking
care to follow instructions depending upon whether you're
using SSI or an HTML image to call the program, compile
on telnet after ftp'ing to be sure it's clean
-
now that the program's on your server and compiles cleanly,
you need to run program04.cgi from your browser.
-
first set the chmod to 755 and then run it.
-
now get on your browser and point to the cgi program as
your url... it should execute and then show you the
cookie it created on your monitor (that's what the second
print statement was for).
-
be sure to now reset the chmod to 744 so nobody else can
execute your program, otherwise you won't be counting
their visits to your site
-
now look at the counter value in your data directory,
then surf your site a bit and look again.
-
of course you might be registering hits because someone
else was surfing at the same time, but if you look at
three pages and register three additional hits, then
try again and it still happens... probably not a
coincidence.
-
finally you can call your best friend and have them look
at your site to be sure the counter works for others.
-
better still, why not get a second browser installed on
your machine? remember that cookies are browser specific,
so if you've got IE and Netscape installed you'll only
be 'uncounted' on IE but not Netscape if it was IE where
you ran the program04.cgi program from
|
|
Debugging Cookie Problems :
|
|
Cookies can be particularly difficult to debug for a
variety of reasons. I use the code supplied here on my own
websites so I know it works, but if you were to write me and
say you did everything and it still doesn't work I'd believe
you.
That's because now you're not only dealing with the server
computer and operating system but also your own client
machine and the browser you happen to be running. To make
matters worse, the command to write a cookie has to be issued
as part of the header information of the CGI program, which
is why we have two separate "\n" carriage control characters
printing.
Look at the program again. It sets the cookie, and then
apparently sets it again. Huh?
Actually the first 'Set-Cookie:' command tells the browser
to create our cookie, the second gets printed to the screen
as plain old HTML text. The two \n characters printed out
between these commands indicate that the header is complete
and we are now sending HTML to the browser when we print.
|
Debugging Tips :
-
when running the program which makes the cookie, let it
finish completely
-
look at the source for the page that program04.cgi
creates and compare it to the program, it should all
make sense since it comes right from the program
-
check your cookie file and see if the information has been
stored there... BUT be aware that cookie info can be cached
in RAM by the browser so to see it written out to a disk
file you may have to quit the browser first
-
if you're failing on program04.cgi and getting no output
to the browser at all, and you've checked the permissions
and the program compiles cleanly on the server (using
telnet) then move the 'print "\n";' line up above the first
set-cookie line. this will permit you to see what's going
on because you won't be trying to set the cookie anymore
-
remember the set-cookie command has to execute before
you can print any HTML out to the browser, so you're kind
of stuck w/o following the suggestion above
-
another way to handle debugging is on the command line.
if you execute the program from telnet you won't be
able to set a cookie because you'll be using telnet
rather than a web browser. however you will be able
to see the variables being set, at least it's some help.
|
|
Part II - Installation on the Web:
|
|
The beauty of a modular approach now shows itself. Previously
we had setup the pages on the site to all use the one program,
either as an SSI or through an HTML image tag. Now we've
altered that program to check for our cookie, and run another
program to create the cookie. That should do it.
|
|
|