Giter Club home page Giter Club logo

php-country-block's People

Contributors

emanuele45 avatar matthewgross avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

php-country-block's Issues

I can only add 2 Countries

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

Solving some errors of the class

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.