When a web page is accessed by a link from some other page, the address of the other page (the "referring page") is made available to the web server. We can pick that information up from logs or as the page is being displayed. For example, if we have Server Side Includes or php, we can pickup the referring page from an environment variable. Here's a snippet of Perl code that does that:
$frompage=$ENV{HTTP_REFERER};
$thispage=$ENV{REQUEST_URI};
Yes, there's a missing R in HTTP_REFERER. Yes, that's wrong, but that's what the variable is so that's what you use.
That's all very interesting, and people have used this information to get an idea of their web popularity or do do cutesy things like "You came here from..". But there's a more important use for backlinks: they may be a source of more information.
Suppose that I write an article about using DVDRAM for backup. It will quickly be indexed by the major search engines, and people using those engines may get directed to my article. Other folks who have written about DVDRAM or Unix backup may notice it, and may link to it from their articles.
Now consider someone searching for information who gets directed to my page by Google. If all I have is my page, and it isn't quite what they are looking for, or if they need more information, it's back to Google for more. However, suppose I had previously harvested the HTTP_REFERER information for those other pages that linked to me and showed those links to the visitor? These would be excellent links for them to follow up on- if the referring page linked to my DVDRAM for Unix backup page, it may very well be strongly related to what they are looking for.
To accomplish that, I need a database that collects backlinks. That's pretty simple to do. Every one of my pages starts something like this:
<HTML> <HEAD><TITLE>DVD-RAM</TITLE> <!--#include virtual="/cgi-bin/header.pl?/Reviews/dvdram.html" --> </HEAD>
The magic part is the "include virtual" that triggers a Server Side Include script called "header.pl". That script does many things, including setting up a consistent style sheet and harvesting http_referrer information.
See Automagic Website for more details on Server Side Include scripting.
The harvesting uses a Perl database to store the referrals. Here's the relevant code:
#!/usr/bin/perl5
$frompage=$ENV{HTTP_REFERER};
$thispage=$ENV{REQUEST_URI};
$thispage =~ s/index.html//;
# normalize index pages
foreach ($frompage) {
next if ($thispage eq $frompage);
# refresh
next if /=/;
# mostly search engine pages
next if /localhost/;
# somebody's personal links
next if /aplawrence.com/;
next if /aplawrence.com/;
next if not /http:/;
# none of my own stuff
$blkey="$thispage|$frompage";
dbmopen %backlink, "/usr/home/aplawren/www/data/backlinks/index", 440;
$backlink{$blkey}=$thispage;
dbmclose BACKLINK;
}
I deliberately don't store certain pages. I'm not interested (not here anyway) in backlinking to articles within my own site. That is something that would be valuable, but I think it needs to be separate from these, so they aren't harvested here.
I also ignore any link that has "=" in it. That actually ignores some pages that are legitimate, but most such links are search engine result pages or other transient links that I do not want to harvest. Here's a typical search engine transient:
/Reviews/communicator47.html|http://www.altavista.com/web?q=netscape+communicator+downloads+%22netscape+communicator+4.7%22&pptt=netscape+communicator%7Fdownloads%7Fnetscape+communicator+4.7&kl=XX
With more thought and effort you could probably cut out the transients without affecting legitimate use of "=" but I didn't bother. I lose a few referrers that way, but I don't clutter things up with thousands of transients.
Once the harvesting code has picked up the links, it's easy to display them. Here's code that displays all of them; I use this on my Referrals page.
#!/usr/bin/perl5
$forced= shift @ARGV;
$frompage=$ENV{HTTP_REFERER};
$thispage=$ENV{REQUEST_URI};
$thispage =~ s/index.html//;
print "Content-TYPE: text/plain\n\n";
dbmopen %backlink, "/usr/home/aplawren/www/data/backlinks/index", undef;
@backlist="";
$x=0;
while (( $one,$two)= each %backlink) {
($a,$link)=split /\|/,$one;
next if $link =~ /=/;
next if $link =~ /localhost/;
$backlist[$x++] = "<li><a href=\"$link\">$link</a> ($two)\n";
}
dbmclose %backlink;
if ($backlist[0]) {
print "<p>Referring pages (off site)<ul> ";
foreach (sort @backlist) {
print;
}
print "</ul>\n";
print "<br>$x";
}
On specific pages,. the following code only lists those backlinks that are specific to this page:
#!/usr/bin/perl5
$frompage=$ENV{HTTP_REFERER};
$thispage=$ENV{REQUEST_URI};
print "Content-TYPE: text/plain\n\n";
dbmopen %backlink, "/usr/home/aplawren/www/data/backlinks/index", undef;
$backlist="";
while (( $one,$two)= each %backlink) {
next if $two ne $thispage;
($a,$link)=split /\|/,$one;
next if $link =~ /=/;
next if $link =~ /localhost/;
$backlist .= "<li><a href=\"$link\">$link</a>\n";
}
dbmclose %backlink;
if ($backlist) {
print "<p>Referring pages (off site)<ul> $backlist</ul>\n";
print "<p>The links above may be useful in finding related material, however they are generated automatically from http headers and therefore can be inaccurate";
print "<p>These are referrals to this specific page. See <a href=\"/Links/referrals.html\">Referrals</a> for a more complete listing of referrer pages";
}
Note that, unlike the referalls page, I don't bother to sort these.
You can see this code in action at the bottom of every article here. If there are no backlinks, nothing appears, but if someone has referenced the page (and someone has used their reference) it will automatically appear.
Publish your articles, comments, book reviews or opinions here!
© October 2001 Tony Lawrence All rights reservedHave you tried Searching this site?
Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates
This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more. We appreciate comments and article submissions.
Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.
Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.
We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.
Click here to add your comments
Don't miss responses! Subscribe to Comments by RSS or by Email
Click here to add your comments
If you want a picture to show with your comment, go get a Gravatar