Created
November 13, 2018 06:34
-
-
Save dovidezra/e66660654bb763f2f6eaea9c5a439f24 to your computer and use it in GitHub Desktop.
Small script used to block some countries (based on ipinfodb.com database)
This file contains hidden or 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 | |
/** | |
* Small script used to block some countries (based on ipinfodb.com database) | |
* | |
* @author @Heavenstar_ | [email protected] | |
* @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