Last active
October 14, 2017 14:53
-
-
Save desbest/956ca169f13f0b90d21d77dcaa7d4cc2 to your computer and use it in GitHub Desktop.
Stop Forum Spam (block abusive ip addresses and emails from signing up)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function get_web_page($url, $cookiesIn = '') | |
{ | |
$options = array( | |
CURLOPT_RETURNTRANSFER => true, // return web page | |
CURLOPT_HEADER => true, //return headers in addition to content | |
CURLOPT_FOLLOWLOCATION => true, // follow redirects | |
CURLOPT_ENCODING => "", // handle all encodings | |
CURLOPT_AUTOREFERER => true, // set referer on redirect | |
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect | |
CURLOPT_TIMEOUT => 120, // timeout on response | |
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects | |
CURLINFO_HEADER_OUT => true, | |
CURLOPT_SSL_VERIFYPEER => false, // Disabled SSL Cert checks | |
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, | |
CURLOPT_COOKIE => $cookiesIn | |
); | |
$ch = curl_init($url); | |
curl_setopt_array($ch, $options); | |
$rough_content = curl_exec($ch); | |
$err = curl_errno($ch); | |
$errmsg = curl_error($ch); | |
$header = curl_getinfo($ch); | |
curl_close($ch); | |
$header_content = substr($rough_content, 0, $header['header_size']); | |
$body_content = trim(str_replace($header_content, '', $rough_content)); | |
$pattern = "#Set-Cookie:\\s+(?<cookie>[^=]+=[^;]+)#m"; | |
preg_match_all($pattern, $header_content, $matches); | |
$cookiesOut = implode("; ", $matches['cookie']); | |
$header['errno'] = $err; | |
$header['errmsg'] = $errmsg; | |
$header['headers'] = $header_content; | |
$header['content'] = $body_content; | |
$header['cookies'] = $cookiesOut; | |
return $header; | |
} | |
// stopforumspam.com API | |
function sfs_ip($ip) | |
{ | |
//$response = file_get_contents('http://www.stopforumspam.com/api?ip='.$ip); //curl is installed | |
$response = get_web_page('http://www.stopforumspam.com/api?ip=' . $ip); //curl is not installed | |
$response = $response['content']; | |
$pattern = '/<appears>yes<\/appears>/'; | |
if (preg_match($pattern, $response)) { | |
return true; | |
//require_once( './stopspammer.html' ); | |
//exit(); | |
} else { | |
return false; | |
} | |
} | |
function sfs_email($email) | |
{ | |
//$response = file_get_contents('http://www.stopforumspam.com/api?email='.$email); //curl is installed | |
$response = get_web_page('http://www.stopforumspam.com/api?email=' . $_POST['email']); //curl is not installed | |
$response = $response['content']; | |
$pattern = '/<appears>yes<\/appears>/'; | |
if (preg_match($pattern, $response)) { | |
return true; | |
//require_once( './stopspammer.html' ); | |
//exit(); | |
} else { | |
return false; | |
} | |
} | |
// ini_set('allow_url_fopen',1); //fix file_get_contents(): http:// wrapper is disabled in the server configuration by allow_url_fopen=0 error | |
$spammer1 = sfs_ip($_SERVER['REMOTE_ADDR']); //check if ip address is a spammer | |
$spammer2 = sfs_email($_POST['email']); // check if email address is a spammer | |
//$response = get_web_page('http://www.stopforumspam.com/api?email='.$_POST['email']); $response = $response['content']; | |
//$response2 = get_web_page('http://www.stopforumspam.com/api?ip='.$_SERVER['REMOTE_ADDR']); $response2 = $response2['content']; | |
//echo "$response // $response2"; | |
if ($spammer1 === true || $spammer2 === true) { | |
$notice = "You cannot signup because you're on a spammer blacklist. To get removed, go to <a href=\"http://stopforumspam.com/remove\">http://stopforumspam.com/remove</a>"; | |
//make the above variable a flash variable | |
header("Location: $_SERVER[HTTP_REFERER] "); //redirect to referrer | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment