This week, I have been trying to tackle my referrer spam. It has gotten terrible. I have looked in a number of places (botwack and diveintomark), but I can't seem to figure it out. I'm guessing the main reason I couldn't figure this out is because I don't really understand it, but I'm wondering if something has gone wrong.

So, could someone help me out please? Here is the source for refer.php (with the server login bits removed) so you can see what I'm up to. If you can help me, I'd really appreciate it.