21
11/10
Know When Someone Links Your Blog
So—being that I’m new at this whole WordPress thing—I was playing around with the blogroll today, and was wondering if it sent PingBacks when I linked someone’s blog (maybe this is something retarded to think, I don’t know, just something I was thinking about since I’ve seen “PingBack from [...]” in so many bloggers’ comments).
Turns out, it doesn’t. I tried both by linking my own blog and by linking a friend’s. Which got me to wondering, how would you know if someone was linking your blog? Go through the IIS logs (Apache sucks and is therefore irrelevant)? That’s lame.
While it may seem immaterial to some people, but I like knowing all the goings on on my web server—because it is mine, none of that hosted garbage, this baby sits in my basement.
So—like all good nerds—I invented a way: A simple PHP script that you include in WordPress’ index.php, so it runs everytime index.php is loaded, and tests for a referrer from a domain it hasn’t seen before (you could easily modify it to look for a page it hasn’t seen before, but I don’t really care about that). When it sees a referrer from such a domain, it logs it to a file—so it doesn’t get all excited about it again—and sends you an e-mail letting you know.
Here’s the script—referrer.php:
<?php
if (isset($_SERVER['HTTP_REFERER'])) {
if (preg_match("#^http://(.+?)/#i",$_SERVER['HTTP_REFERER'],$matches)!=0) {
$handle=fopen("./referrerlist.txt",'r');
$exists=false;
$regexed="#^".preg_replace("#\.#","\.",$matches[1])."$#i";
while (($holdthis=fgets($handle))!=NULL) {
if (preg_match($regexed,$holdthis)!=0) {
$exists=true;
break;
}
}
fclose($handle);
if (!$exists) {
$handle=fopen("./referrerlist.txt",'a');
flock($handle,LOCK_EX);
fwrite($handle,$matches[1]."\n");
flock($handle,LOCK_UN);
fclose($handle);
mail(
"rleahy@rleahy.ca",
"New link to your blog!",
"The domain ".
$matches[1].
" has started linking to your blog, from the URL ".
$_SERVER['HTTP_REFERER'].
"."
);
}
}
}
?>
Obviously you’re going to want to replace my e-mail address with your own.
Once you’ve done this, save it in the root of your WordPress site, and then create an empty text file called referrerlist.txt—this is where the script will store the list of domains.
Last, but not least, open index.php—the heart of WordPress—and add this line:
include('./referrer.php');
Then visit your blog, and click one of the links it has to itself (such as opening a blog entry). You should immediately receive an e-mail notifying you that you’re linking to yourself (lol), and your domain should appear in referrerlist.txt.
I’m currently using it in my blog—which you’re ostensibly reading
—so it doesn’t fuck with your WordPress or anything (unless my WordPress is fucked?).
Oh, and, copyleft et cetera. 


