mpge / php-country-block Goto Github PK
View Code? Open in Web Editor NEWUses the http://ipinfodb.com/ip_location_api.php (ipinfodb.com ip location api) to block certain countries of your choosing.
Uses the http://ipinfodb.com/ip_location_api.php (ipinfodb.com ip location api) to block certain countries of your choosing.
Hi! I'm José Manuel Blasco Galdón, an spanish student/developer. I was trying to use this class but I found some errors, but I could overcome it.
First of all, there is a syntax error in line 113, it can be solved by deleting one those parenthesis ")" (there is one more parenthesis than we need).
Then, I've found one error (other user posted an issue with it here). The class constructor always will return a new instance of countryBlock, not true or false. So, when you try to test it, it will return true because the variable is not null, exists. My way to solve it consists in add a public boolean variable "isBlocked" of the class and change the constructor code to this:
class countryBlock {
// Store Country
private $countries = null;
// Store Visitors IP Address
private $ip_address = null;
// Store ipInfo object
private $ipInfo = null;
// Store if countries are blocked
public $isBlocked = false;
public function __construct($countries, $api_key, $path_to_script = '')
{
// Include ipInfo.inc.php with or without path
include($path_to_script.'ipInfo.inc.php');
// new ipInfo class with api_key from parameters
$this->ipInfo = new ipInfo($api_key);
// Save Countries to $countries
$this->countries = $countries;
// Save IP Address $ip_address
$this->ip_address = $this->ipInfo->getIPAddress();
// Check if cookie exists
if($this->cookieCheck())
{
// returned true... cookie does not exist
foreach($this->countries as $country)
{
// return true or false
$blockable = $this->countryCheck($country);
// check
if($blockable)
{
// block
$this->setCookie();
// HERE WE CHANGE THE RETURN LINE
$this->isBlocked = true;
////////////////////////////////////////////////////////////////
break;
}
}
}
else
{
// don't block.. Already blocked...
// HERE WE CHANGE THE RETURN LINE
$this->isBlocked = true;
////////////////////////////////////////////////////////////////
}
}
By doing this we only have to test if country is blocked this way:
require_once "php/country-block.inc.php";
// Block Sweden
$countries = array("CA", "US");
// Api Key
$api_key = "YOUR_API_KEY"; // Replace to your api key
// return true if someone has been blocked. false if not
$countryBlock = new countryBlock($countries, $api_key);
if($countryBlock->isBlocked) { // returned true
echo "BLOCKED!";
}
else {
echo "NOT BLOCKED :)";
}
And the last error I've found may be because the format of the information returned by ipInfo should have changed since Matthew Gross wrote this class so now he have to change the countryCheck function. To access the two letter country extracted from the user IP we have to explode the string returned with the ";" by delimiter. This way the third element of the array we obtain will be the two letter country we need to compare with the string we provided in the class constructor.
protected function countryCheck($country)
{
// Get Country from ipInfo API
$userCountry = $this->ipInfo->getCountry($this->ip_address);
$exploded = explode(";",$userCountry);
echo $exploded[3];
// Compare...
if($country == $exploded[3])
{
// Should block
return true;
}
else
{
// Shouldn't block
return false;
}
}
For the rest, I have to say thank you so much to Matthew Gross for help with this great class, so easy to use and useful of course.
Maybe I made some mistakes because I'm still studying PHP, but this way is how it works to me.
Greetings, José Manuel Blasco.
I cannot add more than 2 countries. The script doesn't check the third or other countries at all.
I tried like this:
$countries = array("CA","US","CG","SG")
but it only reads CA and US. SG and CG not working at all
It always returning true.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.