In: , ,
On: 2005 / 02 / 05 Viewed: 45215 times
Shorter URL for this post: http://ozh.in/4b

Refer SpamI have been hit quite bad by referer spammers these last few days, with peaks at one hit every 2 seconds (bots loading pages of my site with a fake referer in order to make someone click on their site, have a look at this refer spam screenshot)

It's not like I really care : I don't display publicly my referers and I'm not dumb enough to click on their links, so they are not earning a single click from my site. Plus, I'm bandwith unmetered and uncapped on a fat 100 Mbits, so their bot loading pages is not a real problem. It's just that I'd rather be slashdotted than refer-spam-hammered :)

So I wrote a simple anti refer spam script, sending back the spammers to their own site.

Update 2005-02-25 : I'm currently improving quite a few things in this script, yet having in mind that I want to keep it as easy as possible. Stay tuned if you're interested, feel free to leave a comment asking for a notice when it's available.

Update 2005-09-01 : this script is deprecated. I've made a much much more efficient, smarter, and that require ultra light if any maintenance. I just have to find time to publish it :)

Get the script

The script is rather simple : an array of obvious spammers site (or keywords, or top domain), and a loop comparing the referer with each entry of the array. If one matches, client gets redirected to its own site. Now if they want to hammer themselves once every 2 seconds, I really don't care :)

PHP:
  1. <?php
  2. $spams = array (
  3.   "terashells.com", "chat-nett.com", "exitq.com", "cxa.de", "sysrem03.com",
  4.   "pharmacy.info", "guide.info", "drugstore.info",
  5. ); // array of evil spammers
  6.  
  7. $ref = $_SERVER["HTTP_REFERER"];
  8.  
  9. foreach ($spams as $site) {
  10.     $pattern = "/$site/i";
  11.     if (preg_match ($pattern, $ref)) {
  12.         header("Location: $ref"); exit();
  13.     }
  14. }
  15. ?>

You can either cut and paste the code above, or download the script which contains my real-time updated spammers list (which doesn't grow fast, hopefully) :

Install and customize

To add more entries to the spam array, just add comma separated strings, enclosed with quotes. These strings can be a full url (www.i-am-a-naughty.spammer.com), just the main domain (spammer.com), or even a keyword (poker-online, but be warned that a genuine visitor coming from http://joesblog.com/archives/i-hate-poker-online/ will be bounced back)

Make sure strings contain no slashes, or otherwise modify the script as suggested by Chris in comment #2 (and, by the way, thanks for the tip Chris :)

Unless bandwith is a real problem to you and you are being hit real bad, I'd suggest not to give the list too much attention. Just add entries when you notice a serious spammer, or you are quickly going to manage a 1000 entry array :)

Then, add the following line to the very beginning of the pages you want to protect (in most blog, adding it at the top of index.php in your blog root will protect your whole site)

PHP:
  1. <?php
  2. require('/home/you/blog/physical/path/to/no-refer-spam.php');
  3. ?>

Your file, i.e. index.php, must begin with these 3 lines.

Final words

My script is one solution amongst many. If your host permits it, you can as well use mod_rewrite and add lines to your .htaccess, but I find mod_rewrite rules less user friendly than a straight PHP script.

If you are looking for a neat script to watch your referers, I'd suggest you give Refer a try.

Related posts

Shorter URL

Want to share or tweet this post? Please use this short URL: http://ozh.in/4b

Metastuff

This entry "No Refer Spam" was posted on 05/02/2005 at 3:34 pm and is tagged with , ,
Watch this discussion : Comments RSS 2.0.

30 Blablas

    Pages: [3] 2 1 » Show All

  1. 30
    likejazz Korea (South) »
    said, on 14/Oct/06 at 7:33 pm # :

    I'm NOT spammer. likejazz.com is just written in Korean.

  2. 29
    Ralph United States »
    thought, on 01/Aug/06 at 1:53 pm # :

    Would not it be easier to block unwanted referers through .htaccess?

  3. 28
    Lee Great Britain (UK) »
    replied, on 03/Dec/05 at 1:07 pm # :

    I'd like to know when you update this :)

    To my horror only the other day I started getting refer spam in awstats, about 400 domains...is very depressing.

  4. 27
    Webbericht Europe »
    pingback on 09/Nov/05 at 12:45 pm # :

    [...] Ich werde jetzt mal das Skript von planetOzh testen und hoffen, dass ich damit erst einmal sicher bin. Abgelegt von Kolja Schönfeld unter: Berichte | Webbericht | Spam [...]

  5. 26
    localhost Netherlands »
    commented, on 06/Sep/05 at 1:35 pm # :

    Just mail it. I'm more then willing to post it on my own blog with all needed credits to you.

  6. 25
    Ozh France »
    commented, on 06/Sep/05 at 11:50 am # :

    Hey, it seems like I really have to release the thing now :)
    Marco » no, unfortunately, this doesn't waste spammers bandwidth :) But it saves yours, and your referral log, which is cool anyway :)
    "localhost" » no, it only relies on a PHP script you can include on any PHP powered website.

  7. 24
    localhost Netherlands »
    replied, on 06/Sep/05 at 11:38 am # :

    I would love to see your new solution as well. Hope you don't use the approach Marco has available by using a htaccess file. My host doesn't support this.

  8. 23
    Marco Netherlands »
    wrote, on 05/Sep/05 at 10:57 pm # :

    Ah ok! Well if you've found a way to make spammers eat up their own bandwidth while saving our own I'm extremely interested!

  9. 22
    Ozh France »
    said, on 05/Sep/05 at 8:43 pm # :

    Marco, you're right, and I know this :)
    Actually this script is mostly deprecated. I have written a much more efficient and smarter one, I have yet to find some time to release the thing :)

  10. 21
    Marco Netherlands »
    commented, on 05/Sep/05 at 4:39 pm # :

    Here's what I tried to post, for those who are interested:

    http://www.i-marco.nl/weblog/ozh-why.txt

Pages: [3] 2 1 » Show All

Leave a Reply

Comment Guidelines or Die

  • HTML: You can use these tags: <a href=""> <em> <i> <b> <strong> <blockquote>
  • Posting code: Post raw code (no <> &lt; etc) within appropriate tags : [php][/php], [css][/css], [html][/html], [js][/js], [sql][/sql], [xml][/xml], or generic [code][code]
  • Gravatars: Curious about the little images next to each commenter's name ? Go to Gravatar.
  • Spam: Various spam plugins on patrol. I'll put pins in a Voodoo doll if you spam me.
  • I will mark as Spam test comments, all comments with SEO names (ie "My Cool Online Shop" instead of "Joe") or containing forum-like signatures.

Read more ?

 gre.gario.us » 
Close
E-mail It