separator

Take a quiz or make one of your own

Article 4 - Using a Cookie to Override Your Web Hit Counter

Reading the Cookie :

The first step is to alter the count program so it doesn't register hits when the cookie is found. This can be done by reading the cookie and looking for the value we're going to put in there:

    $ENV{'HTTP_COOKIE'} =~ /id=owner/

The code fragment above retrieves the cookie from the ENV hash and checks if it contains the string 'id=owner'.

Making the change using the SSI version      exit if $ENV{'HTTP_COOKIE'} =~ /id=owner/;

Put that line on top of the code so it executes first, taking care to start after the comments. Now the first thing the program will do is check for 'id=owner' in a browser cookie, and if it finds it the program will stop. If it doesn't find it the program will do its counting thing and then end.

Making the change using the image tag version

     &count if $ENV{'HTTP_COOKIE'} =~ /id=owner/;

Change the line on top of the code so it says this instead of just executing the count subroutine. Now the first thing the program will do is check for 'id=owner' in a browser cookie, and if it finds it the program will not do the count routine.

Whether the cookie is found or not, the transparent 1 pixel image will still be printed. If that didn't happen then you'd get a broken image error on the rendered webpage which looks UGLY on older browsers.

The Program: this program will create a cookie on your browser containing the identification string the previous code is looking for.


#!/usr/local/bin/perl
#
# Cookie Hit Counter Override
#
# Program04 in a series of CGI/Perl Tutorials on
# http://www.thecgibin.com/
# Author : Marty Landman Email : cgiperl@thecgibin.com
#
# You may use and distribute this freely as long as you agree
#
# 1) not to hold Face 2 Interface Inc or Marty Landman
# responsible for the results of using this software
# 2) to keep these comments intact
#

use strict;

print "Content-Type: text/html\n";
$exprtn = 'expires=Wed, 1-Jan-2003 05:00:00 GMT;'; # midnite in NY
$value = "id=owner;";
print "Set-Cookie: $value $exprtn\n";
print "\n";
print "<h1>bye for now<h1> \n";

__END__

separator

© www.thecgibin.com