21

11/10

Referrer Harvesting For ASP.NET

23:17 by rleahy. Filed under: Technology

So I’m happy that I can now see who’s linking to my blog—whether they use PingBacks et cetera or not—which got me thinking that it’d be somewhat cool to do the same thing for my homepage, which is written in ASP.NET.

So here’s code that does exactly the same thing as the PHP code I posted in my last blog post, except with ASP.NET (and C#):

try {

    if (Request.UrlReferrer!=null) {

        Response.Write(
            "<div class=\"categorybox\">You were linked to here from <a href=\""+
            Request.UrlReferrer.ToString()+
            "\">here</a>.</div>"
        );

        Boolean exists=false;
        using (System.IO.StreamReader read=new System.IO.StreamReader(
            new System.IO.FileStream(
                System.IO.Path.Combine(
                    Server.MapPath(null),
                    "referrerlist.txt"
                ),
                System.IO.FileMode.OpenOrCreate,
                System.IO.FileAccess.Read,
                System.IO.FileShare.Read
            )
        )) {

            String holdthis;
            while ((holdthis=read.ReadLine())!=null) {

                if (Request.UrlReferrer.Host==holdthis) {

                    exists=true;
                    break;

                }

            }

        }

        if (!exists) {

            using (System.IO.StreamWriter write=new System.IO.StreamWriter(
                new System.IO.FileStream(
                    System.IO.Path.Combine(
                        Server.MapPath(null),
                        "referrerlist.txt"
                    ),
                    System.IO.FileMode.Append,
                    System.IO.FileAccess.Write,
                    System.IO.FileShare.None
                )
            )) {

                write.WriteLine(Request.UrlReferrer.Host);

            }

            System.Net.Mail.SmtpClient mail=new System.Net.Mail.SmtpClient("192.168.50.4",25);
            mail.Send(
                "noreply@rleahy.ca",
                "rleahy@rleahy.ca",
                "Your Website Has Been Linked To",
                "The domain "+
                    Request.UrlReferrer.Host+
                    " has started linking to your site from "+
                    Request.UrlReferrer.ToString()+
                    "."
            );

        }

    }

} catch { }

Since .NET’s SmtpClient doesn’t pull from a configuration file–unlike PHP’s mail, which pulls SMTP settings from php.ini—there are few more “fill in the blanks” as opposed to yesterday:

  • Your SMTP server’s IP/hostname.
  • Your SMTP server’s port.
  • The from address for your server.
  • The to address (just like the PHP script).

If you want to be a real keener—or if it just fits in with your application—you can replace these “blanks” with calls to ConfigurationManager.AppSettings and store all of that in your web.config file.

Other than that, just put it between <% and %> anywhere in your *.aspx file, and you’re set.

If your environment uses authentication or SSL for SMTP, read the SmtpClient documentation and adjust the mailer portion accordingly.

Afterthought: This is the first blog post I’ve made before midnight…what a night owl I am…

21

11/10

Know When Someone Links Your Blog

05:23 by rleahy. Filed under: Technology
Tags: , ,

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.