I've been using this code for a few weeks now and spammer activity has dropped off almost completely.
It's not 100%, some still get through, but instead of 5-6 spammer registrations per day, I get 2-3 per month.
It also sends you an email to confirm that it's blocked a spammer, (if you enable it).
It checks with stopforumspam.com to verify the ip, and if they are listed, they are redirected to a new page, telling them how to unlist their ip.
The code consists of 2 files, and one line to edit into a page of your choice.
It works on all types of software, including forums, galleries, blogs, portals, and much much more.
Please check all links in the code otherwise it will fail.
spammertrap.php
<?php
// SpammerTrap - JACP - endtimesroundtable.com / etrtmedia.com / watcherspace.com
// Using http://www.bin-co.com/php/scripts/load/
// Version : 1.00.A Curl/Fsockets Routine
// ==============================
// Turn on (=1) or off (=0) email notification
$nudgeme = 0;
// Get IP from client
$whitetest = ipCheck();
// Begin Whitelist
$whiteskip = 0;
// Add this test section for each IP to force acceptance
if ($whitetest == "0.0.0.0")
{
$whiteskip = 1;
}
// End of test section
// Do the rest if nothing found on whitelist
if ($whiteskip == 0)
{
// Use StopForumSpam API
$contents = load("http://www.stopforumspam.com/api?ip=$whitetest");
// Check for a positive result, which means a spammer IP has been detected
$mycheck = stripos($contents, "<appears>yes</appears>");
if ($mycheck !== false)
{
// Send them far away from here
header('Location: http://www.yourdomain/linkto/spammertrap.html');
// Change the above URL to a localized webpage, or use the one provided
if ($nudgeme == 1)
{
// If the $nudgeme is set to 1, send an email alert
// Recipient - change to your notification email address
$to = "your-email@yourdomain.com";
// Subject
$subject = 'SpammerTrap Alert.';
// Message
$message = "
<html>
<head>
<title>SpammerTrap Alert</title>
</head>
<body>
$whitetest has been detected by the Stop Forum Spam API as being a spammer.
</body>
</html>
";
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers - change this to be the main webmaster email for your board
$headers .= 'From: youremail@yourdomain.com' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
}
}
// Not found, back to normal activities
}
// End of program code, functions follow
function ipCheck()
{
if (getenv('HTTP_CLIENT_IP')) {
$ip = getenv('HTTP_CLIENT_IP');
}
elseif (getenv('HTTP_X_FORWARDED_FOR')) {
$ip = getenv('HTTP_X_FORWARDED_FOR');
}
elseif (getenv('HTTP_X_FORWARDED')) {
$ip = getenv('HTTP_X_FORWARDED');
}
elseif (getenv('HTTP_FORWARDED_FOR')) {
$ip = getenv('HTTP_FORWARDED_FOR');
}
elseif (getenv('HTTP_FORWARDED')) {
$ip = getenv('HTTP_FORWARDED');
}
else {
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
function load($url,$options=array('method'=>'get','return_info'=>false)) {
$url_parts = parse_url($url);
$info = array(//Currently only supported by curl.
'http_code' => 200
);
$response = '';
$send_header = array(
'Accept' => 'text/*',
'User-Agent' => 'BinGet/1.00.A (http://www.bin-co.com/php/scripts/load/)'
);
///////////////////////////// Curl /////////////////////////////////////
//If curl is available, use curl to get the data.
if(function_exists("curl_init")
and (!(isset($options['use']) and $options['use'] == 'fsocketopen'))) { //Don't user curl if it is specifically stated to user fsocketopen in the options
if(isset($options['method']) and $options['method'] == 'post') {
$page = $url_parts['scheme'] . '://' . $url_parts['host'] . $url_parts['path'];
} else {
$page = $url;
}
$ch = curl_init($url_parts['host']);
curl_setopt($ch, CURLOPT_URL, $page);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //Just return the data - not print the whole thing.
curl_setopt($ch, CURLOPT_HEADER, true); //We need the headers
curl_setopt($ch, CURLOPT_NOBODY, false); //The content - if true, will not download the contents
if(isset($options['method']) and $options['method'] == 'post' and $url_parts['query']) {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $url_parts['query']);
}
//Set the headers our spiders sends
curl_setopt($ch, CURLOPT_USERAGENT, $send_header['User-Agent']); //The Name of the UserAgent we will be using ;)
$custom_headers = array("Accept: " . $send_header['Accept'] );
if(isset($options['modified_since']))
array_push($custom_headers,"If-Modified-Since: ".gmdate('D, d M Y H:i:s \G\M\T',strtotime($options['modified_since'])));
curl_setopt($ch, CURLOPT_HTTPHEADER, $custom_headers);
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt"); //If ever needed...
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
if(isset($url_parts['user']) and isset($url_parts['pass'])) {
$custom_headers = array("Authorization: Basic ".base64_encode($url_parts['user'].':'.$url_parts['pass']));
curl_setopt($ch, CURLOPT_HTTPHEADER, $custom_headers);
}
$response = curl_exec($ch);
$info = curl_getinfo($ch); //Some information on the fetch
curl_close($ch);
//////////////////////////////////////////// FSockOpen //////////////////////////////
} else { //If there is no curl, use fsocketopen
if(isset($url_parts['query'])) {
if(isset($options['method']) and $options['method'] == 'post')
$page = $url_parts['path'];
else
$page = $url_parts['path'] . '?' . $url_parts['query'];
} else {
$page = $url_parts['path'];
}
$fp = fsockopen($url_parts['host'], 80, $errno, $errstr, 30);
if ($fp) {
$out = '';
if(isset($options['method']) and $options['method'] == 'post' and isset($url_parts['query'])) {
$out .= "POST $page HTTP/1.1\r\n";
} else {
$out .= "GET $page HTTP/1.0\r\n"; //HTTP/1.0 is much easier to handle than HTTP/1.1
}
$out .= "Host: $url_parts[host]\r\n";
$out .= "Accept: $send_header[Accept]\r\n";
$out .= "User-Agent: {$send_header['User-Agent']}\r\n";
if(isset($options['modified_since']))
$out .= "If-Modified-Since: ".gmdate('D, d M Y H:i:s \G\M\T',strtotime($options['modified_since'])) ."\r\n";
$out .= "Connection: Close\r\n";
//HTTP Basic Authorization support
if(isset($url_parts['user']) and isset($url_parts['pass'])) {
$out .= "Authorization: Basic ".base64_encode($url_parts['user'].':'.$url_parts['pass']) . "\r\n";
}
//If the request is post - pass the data in a special way.
if(isset($options['method']) and $options['method'] == 'post' and $url_parts['query']) {
$out .= "Content-Type: application/x-www-form-urlencoded\r\n";
$out .= 'Content-Length: ' . strlen($url_parts['query']) . "\r\n";
$out .= "\r\n" . $url_parts['query'];
}
$out .= "\r\n";
fwrite($fp, $out);
while (!feof($fp)) {
$response .= fgets($fp, 128);
}
fclose($fp);
}
}
//Get the headers in an associative array
$headers = array();
if($info['http_code'] == 404) {
$body = "";
$headers['Status'] = 404;
} else {
//Seperate header and content
$separator_position = strpos($response,"\r\n\r\n");
$header_text = substr($response,0,$separator_position);
$body = substr($response,$separator_position+4);
foreach(explode("\n",$header_text) as $line) {
$parts = explode(": ",$line);
if(count($parts) == 2) $headers[$parts[0]] = chop($parts[1]);
}
}
if($options['return_info']) return array('headers' => $headers, 'body' => $body, 'info' => $info);
return $body;
}
?>
This is the redirction page.
spammertrap.html
<html>
<head>
<title>Spammer IP Address Detected</title>
</head>
<body bgcolor="red">
<h1>Attention!</h1>
<h2>Your IP address has been identified as belonging to a known spammer.</h2>
<hr>
<p>If you feel this is in error, please contact the webmaster of the site you were just viewing.</p>
<br>
<p>Also, please contact the <u>Stop Forum Spam</u> website using their help form <a href="http://www.stopforumspam.com/contact">HERE.</a></p>
<p>Inform them that your IP address is being incorrectly flagged as a spammer, and you'd like to have it removed.</p>
<br>
<center>
<i>Thank You!</i>
</center>
</body>
</html>
And this is the single line of code needed in your template, you can out this line just about anywhere on your site.
<script type="text/javascript" src="http://www.yourdomain.com/linkto/spammertrap.php"></script>
I've posted the code, as I only have winrar installed and I cant post RAR files. Sorry.
Thanks to Stoker at
http://www.phpbb3bbcodes.com/portal.php for posting it originally.
Sorry if it's a dupe, i could'nt find it in search, so i think i'm the first with this.