{"id":193,"date":"2005-02-05T15:34:22","date_gmt":"2005-02-05T14:34:22","guid":{"rendered":"http:\/\/frenchfragfactory.net\/ozh\/archives\/2005\/02\/05\/avoid-refer-spam\/"},"modified":"2007-05-09T23:41:02","modified_gmt":"2007-05-09T21:41:02","slug":"no-refer-spam","status":"publish","type":"post","link":"https:\/\/planetozh.com\/blog\/2005\/02\/no-refer-spam\/","title":{"rendered":"No Refer Spam"},"content":{"rendered":"<p><a href=\"\/blog\/images\/refer_spam.gif\"><img decoding=\"async\" src=\"\/blog\/images\/refer_spam-s.gif\" alt=\"Refer Spam\" class=\"imgleft\" \/><\/a>I have been hit quite bad by referer spammers these last few days, with peaks at <strong>one hit every 2 seconds<\/strong> (bots loading pages of my site with a fake referer in order to make someone click on their site, have a look at this <a href=\"\/blog\/images\/refer_spam.gif\">refer spam screenshot<\/a>)<\/p>\n<p>It&#39;s not like I really <em>care<\/em> : I don&#39;t display publicly my referers and I&#39;m not dumb enough to click on their links, so they are not earning a single click from my site. Plus, I&#39;m bandwith unmetered and uncapped on a fat 100 Mbits, so their bot loading pages is not a real problem. It&#39;s just that I&#39;d rather be slashdotted than refer-spam-hammered :)<\/p>\n<p>So I wrote a simple anti refer spam script, sending back the spammers to their own site.<\/p>\n<p><!--more--><\/p>\n<p><strong>Update 2005-02-25<\/strong> : I&#39;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&#39;re interested, feel free to leave a comment asking for a notice when it&#39;s available.<\/p>\n<p><strong>Update 2005-09-01<\/strong> : this script is deprecated. I&#39;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 :)<\/p>\n<h2>Get the script<\/h2>\n<p>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&#39;t care :)<\/p>\n<div id=\"ig-sh-1\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">php<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"php\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000000;font-weight: bold\">&lt;?php<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000088\">$spams<\/span> <span style=\"color: #339933\">=<\/span> <span style=\"color: #990000\">array<\/span> <span style=\"color: #009900\">&#040;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; <span style=\"color: #0000ff\">&quot;terashells.com&quot;<\/span><span style=\"color: #339933\">,<\/span> <span style=\"color: #0000ff\">&quot;chat-nett.com&quot;<\/span><span style=\"color: #339933\">,<\/span> <span style=\"color: #0000ff\">&quot;exitq.com&quot;<\/span><span style=\"color: #339933\">,<\/span> <span style=\"color: #0000ff\">&quot;cxa.de&quot;<\/span><span style=\"color: #339933\">,<\/span> <span style=\"color: #0000ff\">&quot;sysrem03.com&quot;<\/span><span style=\"color: #339933\">,<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; <span style=\"color: #0000ff\">&quot;pharmacy.info&quot;<\/span><span style=\"color: #339933\">,<\/span> <span style=\"color: #0000ff\">&quot;guide.info&quot;<\/span><span style=\"color: #339933\">,<\/span> <span style=\"color: #0000ff\">&quot;drugstore.info&quot;<\/span><span style=\"color: #339933\">,<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span> <span style=\"color: #666666;font-style: italic\">\/\/ array of evil spammers<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000088\">$ref<\/span> <span style=\"color: #339933\">=<\/span> <span style=\"color: #000088\">$_SERVER<\/span><span style=\"color: #339933\">&amp;<\/span><span style=\"color: #666666;font-style: italic\">#91;&quot;HTTP_REFERER&quot;&amp;#93;;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #b1b100\">foreach<\/span> <span style=\"color: #009900\">&#040;<\/span><span style=\"color: #000088\">$spams<\/span> <span style=\"color: #b1b100\">as<\/span> <span style=\"color: #000088\">$site<\/span><span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #009900\">&#123;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #000088\">$pattern<\/span> <span style=\"color: #339933\">=<\/span> <span style=\"color: #0000ff\">&quot;\/<span style=\"color: #006699;font-weight: bold\">$site<\/span>\/i&quot;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #b1b100\">if<\/span> <span style=\"color: #009900\">&#040;<\/span><span style=\"color: #990000\">preg_match<\/span> <span style=\"color: #009900\">&#040;<\/span><span style=\"color: #000088\">$pattern<\/span><span style=\"color: #339933\">,<\/span> <span style=\"color: #000088\">$ref<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #009900\">&#123;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: #990000\">header<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #0000ff\">&quot;Location: <span style=\"color: #006699;font-weight: bold\">$ref<\/span>&quot;<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span> <span style=\"color: #990000\">exit<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #009900\">&#125;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #009900\">&#125;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000000;font-weight: bold\">?&gt;<\/span><\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<p>You can either cut and paste the code above, or download the script which contains my real-time updated spammers list (which doesn&#39;t grow fast, hopefully) :<\/p>\n<ul>\n<li><a href=\"\/download\/no-refer-spam.txt\">no-refer-spam.txt<\/a> : save as .php<\/li>\n<li><a href=\"\/download\/no-refer-spam.php\">no-refer-spam.php<\/a> : copy and paste in a blank .php, but don&#39;t download it directly, this is highlighted html, not plain code<\/li>\n<\/ul>\n<h2>Install and customize<\/h2>\n<p>To add more entries to the spam array, just add comma separated strings, enclosed with quotes. These strings can be a full url (<em>www.i-am-a-naughty.spammer.com<\/em>), just the main domain (<em>spammer.com<\/em>), or even a keyword (<em>poker-online<\/em>, but be warned that a genuine visitor coming from <em>http:\/\/joesblog.com\/archives\/i-hate-poker-online\/<\/em> will be bounced back)<\/p>\n<p>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 :)<\/p>\n<p>Unless bandwith is a real problem to you and you are being hit real bad, I&#39;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 :)<\/p>\n<p>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)<\/p>\n<div id=\"ig-sh-2\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">php<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"php\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000000;font-weight: bold\">&lt;?php<\/span> <\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #b1b100\">require<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #0000ff\">'\/home\/you\/blog\/physical\/path\/to\/no-refer-spam.php'<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000000;font-weight: bold\">?&gt;<\/span><\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<p>Your file, i.e. index.php, must begin with these 3 lines.<\/p>\n<h2>Final words<\/h2>\n<p>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 <em>user friendly<\/em> than a straight PHP script.<\/p>\n<p>If you are looking for a neat script to watch your referers, I&#39;d suggest you give <a href=\"http:\/\/www.textism.com\/tools\/refer\/\">Refer<\/a> a try.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>My simple script to avoid refer spam<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[2,10,27],"class_list":["post-193","post","type-post","status-publish","format-standard","hentry","tag-code","tag-php","tag-spam"],"_links":{"self":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts\/193","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/comments?post=193"}],"version-history":[{"count":0,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts\/193\/revisions"}],"wp:attachment":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/media?parent=193"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/categories?post=193"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/tags?post=193"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}