MayoMayn
BestDev
- Oct 18, 2016
- 1,423
- 683
Since y'all got me in a good mood, I'm going to release this script that I coded.
For more information about how this service works check out the website that checks the IP's:
Okay, so first off we gotta create a function called getIP()
Okay, so in the same class, create a function called curl()
So for the second last step, we gotta create a function called validIP() which validates the visitors IP
Run this SQL as a query to create the table needed.
For this to work, you gotta recode the MySQL queries so it works for your own service.
By researching, you can create the most amazing stuff.
For more information about how this service works check out the website that checks the IP's:
You must be registered for see links
Okay, so first off we gotta create a function called getIP()
PHP:
public function getIP()
{
//Just get the headers if we can or else use the SERVER global
if(function_exists('apache_request_headers')) {
$headers = apache_request_headers();
} else {
$headers = $_SERVER;
}
//Get the forwarded IP if it exists
if(array_key_exists('X-Forwarded-For', $headers) && filter_var($headers['X-Forwarded-For'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
$ip = $headers['X-Forwarded-For'];
} elseif(array_key_exists('HTTP_X_FORWARDED_FOR', $headers) && filter_var($headers['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
$ip = $headers['HTTP_X_FORWARDED_FOR'];
} else {
$ip = filter_var($_SERVER['REMOTE_ADDR'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
}
return $ip;
}
Okay, so in the same class, create a function called curl()
PHP:
public function curl($url, $json=false)
{
//Check if server has curl library installed
if(function_exists('curl_init')) {
// Initiate curl
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it prints the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL, $url);
// Execute
$response = curl_exec($ch);
// Close curl
curl_close($ch);
} else {
// Ordinary get content
$response = file_get_contents($url);
}
if($json == true) {
//Return as JSON data.
$response = json_decode($response, true);
}
return $response;
}
So for the second last step, we gotta create a function called validIP() which validates the visitors IP
PHP:
public function validIP()
{
$db = $this->db;
$config = $this->config;
$uip = $this->getIP();
//Check if session variables are set
if(isset($_SESSION['ip_banned']) && isset($_SESSION['visitor_ip']) && $_SESSION['visitor_ip'] === $uip) {
if($_SESSION['ip_banned'] == 0) {
$ipBanned = 0;
$status = true;
} else {
$ipBanned = 1;
$status = false;
}
} else {
//Check if Visitor exists inside our Database Table
$stmt = $db->query("SELECT `banned`,`result` FROM `visitor_ips` WHERE `ip` = :ip LIMIT 1");
$stmt->bindParam(':ip', $uip, $db->PARAM_STR);
$stmt->execute();
if($stmt->rowCount() > 0) {
$ip = $db->assoc($stmt);
if($ip['banned'] == '0' && $ip['result'] < '1') {
$ipBanned = 0;
$status = true;
} else {
$ipBanned = 1;
$status = false;
}
} else {
//Get url for IP Validation
$url = 'https://check.getipintel.net/check.php?ip='.$uip.'&flags=f&format=json&oflags=bc&contact='.$config->hotel('mail');
//Initiate
$json = $this->curl($url, true);
if($json['status'] == "success") {
if($json['BadIP'] == "0") {
$continue = true;
if($config->hotel('c_allow_check') == true) {
if(!in_array($json['Country'], $config->hotel('c_allow'))) {
$continue = false;
$ipBanned = 1;
}
}
if($continue == true) {
if($json['result'] >= '1') {
$ipBanned = 1;
} else {
$ipBanned = 0;
}
}
} else {
$ipBanned = 1;
}
} else {
$ipBanned = 0;
}
// If Visitor does not exists in our table insert them into.
$stmt = $db->query("INSERT INTO `visitor_ips` (`ip`,`banned`,`result`,`agent`) VALUES (:ip,:b,:r,:a)");
$data = [
':ip' => $uip,
':b' => $ipBanned,
':r' => $json['result'],
':a' => $_SERVER['HTTP_USER_AGENT']
];
if($stmt->execute($data)) {
if($ipBanned == 0) {
$status = true;
} else {
$status = false;
}
}
}
$_SESSION['visitor_ip'] = $uip;
$_SESSION['ip_banned'] = $ipBanned;
}
return $status;
}
Run this SQL as a query to create the table needed.
Code:
DROP TABLE IF EXISTS `visitor_ips`;
CREATE TABLE `visitor_ips` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`ip` varchar(45) NOT NULL,
`banned` enum('0','1') NOT NULL,
`result` varchar(255) NOT NULL,
`agent` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
By researching, you can create the most amazing stuff.
Last edited: