Hey, pals, backlinking is all the rage! Not only are the cool kids doing it, even the dorky kids have caught on!

If you’d like some fancy little backlinks to appear on your site, here’s some homegrown PHP code you’re welcome to…


Version 1.11 (July 16, 2002)

<?
$recentfile = "/full/path/to/backlink.txt";
$mydomain = "laze.net"; // REPLACE WITH YOURS
$displaynum = 20;
$displaychar = 24;
$recent = file($recentfile);
$start = 0;
$amount = count($recent);
if ($amount >= $displaynum) { $start = 1; $amount = $displaynum; }
for ($x = ($amount-1); $x >= 0; $x--) {
if (eregi("google\.",$recent[$x]) || eregi("search\.msn\.",$recent[$x]) ||
eregi("atomz\.com",$recent[$x]) || eregi("lycos\.",$recent[$x]) ||
eregi("altavista\.",$recent[$x])) {
if (eregi("google\.yahoo",$recent[$x])) {
list($first,$rest) = split("p=", $recent[$x]);
}
elseif (eregi("lycos\.",$recent[$x])) {
list($first,$rest) = split("query=", $recent[$x]);
}
else {
list($first,$rest) = split("q=", $recent[$x]);
}
list($temp,$toss) = split("&", $rest);
$temp = eregi_replace ("\+", " ", $temp);
$temp = eregi_replace ("%22", "\"", $temp);
$temp = eregi_replace ("%27", "'", $temp);
$temp = eregi_replace ("%26", "&", $temp);
$temp = eregi_replace ("%20", " ", $temp);
if (eregi("atomz\.com",$recent[$x])) {
echo "<b>ls: </b> ";
}
else {
echo "<b>s: </b> ";
}
echo "<a href=\"".rtrim($recent[$x])."\">".substr($temp,0,$displaychar - 3);
if (strlen($temp) > $displaychar - 3) { echo "..."; }
echo "</a><br />\n";
}
else {
$temp = rtrim(substr((eregi_replace("www\.","",$recent[$x])),7,$displaychar));
echo "<a href=\"".rtrim($recent[$x])."\">".$temp;
if (strlen($temp) > $displaychar - 1) { echo "..."; }
echo "</a><br />\n";
}
}
$recent[$amount] = $HTTP_REFERER."\n";
if ($HTTP_REFERER && !(eregi($mydomain,$HTTP_REFERER)) &&
!(eregi("Outpost",$HTTP_REFERER))) {
$fp = fopen ($recentfile, "w");
for ($y = $start; $y <= $amount; $y++) {
fputs($fp, "$recent[$y]");
}
fclose ($fp);
}
?>

Notes:

  • $recentfile should be the full path to the text file that will store your backlinks (it’s a good idea to create an empty file by hand and chmod a+w it so that both you and the webserver can write to the file)
  • $mydomain is the domain most of your pages are hosted on, without the leading “www.” (ex. laze.net).
  • $displaynum is how many backlinks you wish to display
  • $displaychar is the maximum number of characters to display of the link. The “http://” part will not be shown. Anything beyond the number you set will be replaced with ellipses.
  • && !(eregi(“google.com”,$HTTP_REFERER)) blocks out any references from google.com. You can change this or remove it. If you want to add other strings (such as your own domain), add another && !(eregi(“yourdomain.com”,$HTTP_REFERER)) before the final parentheses in the if statement and replace “yourdomain.com” with the string you want to ignore.
  • Note that this isn’t anywhere near as fancy as the perl script on Stephen’s Web or Disenchanted’s Linkback, but perhaps it will be with time. Bookmark this page.
  • If you’re using this code in a PHP includes, make sure that you call the includes with its full pathname (“/path/to/includes.php”) and not the URL (“http://www.yoursite.com/includes.php”). Otherwise, the HTTP_REFERER will be incorrect.

Release notes:

  • 1.0: Initial release. [26 Jun 02]
  • 1.01: Added a couple rtrims for cleaner code. [27 Jun 02]
  • 1.1: Display is now significantly cleaner. References from search engines show a s: and then the search phrase. References from Atomz show a ls: and the search phrase (referencing “local search”). [10 Jul 02]
  • 1.11: Added MSN to search engines and replacing of “%20″ with ” “. [16 Jul 02]

Questions? Leave ’em here.