Skip to content

Instantly share code, notes, and snippets.

@dovidezra
Created November 13, 2018 06:34
Show Gist options
  • Save dovidezra/e66660654bb763f2f6eaea9c5a439f24 to your computer and use it in GitHub Desktop.
Save dovidezra/e66660654bb763f2f6eaea9c5a439f24 to your computer and use it in GitHub Desktop.
Small script used to block some countries (based on ipinfodb.com database)
<?php
/**
* Small script used to block some countries (based on ipinfodb.com database)
*
* @author @Heavenstar_ | alexis.chevalier1@gmail.com
* @version 1.0.0
*/
$apiKey = 'REPLACE WITH YOUR KEY'; //Get an API key here : http://ipinfodb.com/login.php
$apiEndpoint = 'http://api.ipinfodb.com/v3/ip-country/';
$bannedCountries = ['URKAINE', 'UNITED STATES', 'CHINA']; //Replace with your countries
$redirectURI = 'YOUR REDIRECT URI'; //Replace with your redirect URI
try {
$ctx = stream_context_create(array('http'=>
array(
'timeout' => 2, // 2 seconds before timing out
)
));
//Fetching location data from ipinfodb's database (@ is for suppressing errors)
$data = @file_get_contents($apiEndpoint . '?key=' . $apiKey . '&ip=' . $_SERVER['REMOTE_ADDR'], false, $ctx);
if($data !== FALSE) {
$parts = explode(';', $data);
if(isset($parts[4])) {
$country = $parts[4];
for($i = 0; $i < sizeof($bannedCountries); $i++) {
if($country == $bannedCountries[$i]) {
header("Location: " . $redirectURI);
exit();
}
}
}
}
} catch (Exception $e) {}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment