Insert Tracking Information into Your Pages
Occasionally you may wish to insert a visitor's IP address
or other tracking information into a web page for the
visitor to see. For example, on sweepstakes or other pages
that might attract those with a predisposition to fraud, you
might want to say, "Your IP Address ______ is being logged."
Your page will have placeholders where the custom
information is to be inserted. For example, where you
want the visitor's IP address inserted, put:
[[REMOTE_ADDR]]
Other information available for insertion is listed below.
The way it works is that a CGI script retrieves your page,
replaces your placeholders with the actual information,
and sends the modified page to the visitor's browser.
The Script
Here is the ten-line script that does the work:
#!/usr/bin/perl
my $Default = 'http://domain.com/page.html';
use LWP::Simple;
my $URL = $ENV{QUERY_STRING};
$URL = $Default unless $URL =~ m!^https?://.+!i;
my $Page = get $URL;
$Page = qq(URL "$URL" not available.) unless $Page;
for(keys %ENV) { $Page =~ s/\[\[$_\]\]/$ENV{$_}/sig; }
$Page =~ s/\[\[.*?\]\]//sg;
print "Content-Type: text/html\n\n$Page";
The first line is the location of Perl on your server.
Edit the line if needed.
The second line contains the URL of the page the script will
display if no other URL has been specified. Edit the URL.
The third line loads the Perl module LWP::Simple. This
module lets the script retrieve pages from the internet.
Line four grabs the URL specified when the script's link
was clicked on (see "The Script Link," below). If no URL
was specified or if the URL did not begin with "http://"
or "https://" then the default URL is assumed on line
five.
Line six retrieves the page from the internet. If it is
a 404 or some other reason prevents the page from being
retrieved, line seven provides default text. You may edit
the default text between the parenthesis, if you wish.
Line eight replaces your placeholders with custom
information. Line nine strips any placeholders that might
be left in the page when the information was not available.
And line ten sends the page to the browser.
Edit the script with an ASCII/plain text word processor and
save it as insert.cgi or whatever other name makes sense to
you. Upload it to your server as ASCII/plain text and set
global execution permissions (0755).
The Script Link
The link to the script is its URL:
<a href="http://domain.com/cgi-bin/insert.cgi">
click here
</a>
If you want to display a page other than the one at the
default URL, add a question mark to the above URL followed
by the URL of the page you want to display. The link URL
would be like this:
http://domain.com/cgi-bin/insert.cgi?http://domain.com/page.html
Preparing the Page
The page that will be displayed with insert.cgi might need
a bit of special preparation. If the page has images, each
scr="______" must be a complete http://... URL. Same with
sound, form action="____", and the URLs of external style
sheet and JavaScript files.
In other words, every URL on the page must be a complete
http://... URL. That's because the page will be displayed
from within insert.cgi -- the script is likely to be in a
different directory than the page being displayed, so
relative URLs will not work.
There is an exception to the "every URL" rule: Server Side
Includes will work without modification. That's because SSI
commands are completed before insert.cgi receives the page.
Information Available for Insertion
Different servers can provide different information
for your pages. Some servers have a lot of data available
for your use. Use Master Pre-Installation Tester
and select "Report environment variables"
to see a complete list for your server.
Some information is available on most servers. Here are
three:
This code Is replaced with
------------------ ----------------------------------
[[REMOTE_ADDR]] The visitor's IP address.
[[HTTP_REFERER]] The referring URL (if available).
[[HTTP_USER_AGENT]] A cryptic synopsis of the visitor's
operating system and browser types.
Note that the referring URL is not available unless the
visitor arrives at insert.cgi by clicking on a link on a
web page. Further, some browsers can turn off tracking
information which tends to eliminate referrer information
that would otherwise be available.
Demonstration
A demonstration page is
here.
Question:
Did you find this article interesting and understandable? How can it be improved?
Your response is anonymous.
When done typing, click anywhere outside the box. [more info]
Will Bontrager
©2001 Bontrager Connection, LLC
Please note:
Articles on this website are presented "as is". However -
If you have a question about a CGI script, HTML, CSS, PHP, or JavaScript
Ask one of our Experts and you'll have your answer!
Click here for details.