After coming back to this problem an reading tachyon-II's reply, I wondered if this couldn't simply be generalized away for every web file or script (from whatever source), with the goal of inserting a small image with the number of hits for every single page link on a site. The single Perl-Script (eg. in /cgi-bin/) behind this 'image-link' would simply find the link in question (to be counted up) in the $ENV{HTTP_REFERER} variable, handle the database stuff in the background and respond by creating an image (containing a number) on the fly.
This would imply:
1) generate an appropriate link scheme, like this:
[any html or script here]
...
<img src="/cgi-bin/counter.pl/image.png" />
</body>
</html>
where the appendix /image.png is a $ENV{PATH_INFO} to help Internet Explorer to "do the right thing" ;-)
2) we need an appropriate database scheme, for MySQL this would (assumed the database is named 'counting') read:
...
$dbh->do( q{
CREATE TABLE counters
( pagename VARCHAR(640) NOT NULL
, pagecounter INT(11) DEFAULT 0
, PRIMARY KEY (pagename)
) Engine=InnoDB charset=latin1 COMMENT='pagecounter'
}
);
...
... so I chose the link (pagename) to be the PK of the table. This later enables MySQL to do an INSERT IF NOT EXISTS equivalent thing (its actually: 'INSERT IGNORE INTO counters (pagename) VALUES (?)', which is simply ignored if the page entry (PK!) exists.
3) The script, which is straightforward. First, the database is updated (my DBI is rusty, please correct my glitches), then the hit-number image is created (via Imager) and sent to the browser. Database access stuff and a font file path for number-output has to be modified. '/cgi-bin/counter.pl':
I did set this up on my (Linux) Box and it works nice. So I decided to post it, maybe its of use for someone else.
Regards
mwa
|