user6224087
user6224087

Reputation:

PHP blocking website access by country

I want to block some countries accessing my website due to over malicious hits everyday. I have found a script here http://azuliadesigns.com/blocking-website-access-country-php/ where I can block a country from accessing the website. This seems okay for beginning. But here, I can block only one country with if($two_letter_country_code=="US").

Script

if ($_SERVER['HTTP_X_FORWARDED_FOR'])
  $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
else
  $ip   = $_SERVER['REMOTE_ADDR'];

$two_letter_country_code=iptocountry($ip);

function iptocountry($ip)
{
  $numbers = explode( ".", $ip);    

  include("ip_files/".$numbers[0].".php");
  $code=($numbers[0] * 16777216) + ($numbers[1] * 65536) + ($numbers[2] * 256) + ($numbers[3]);    

  foreach($ranges as $key => $value)
  {
    if($key<=$code)
    {
      if($ranges[$key][0]>=$code)
      {
        $country=$ranges[$key][1];break;
      }
    }
  }

  if ($country=="")
  {
    $country="unknown";
  }

  return $country;
}
if ($two_letter_country_code=="US")
  die();

My question is, what if I want to block access to multiple countries here from the database? Suppose, I have saved some country codes in my database to block them say US,IN,PK.... and so on, and I want to block access to all those. In that case, how can I modify this script to work?

What I think should work:

$country_codes_to_block = $row['block_countries']; // Fetched from database. This will give me the country codes US,IN,PK... etc.

Now modifying this line

if ($two_letter_country_code=="US")

to

if (in_array($two_letter_country_code, $country_codes_to_block))

Will this be blocking access to all the countries whose codes are given stored in database? Or should I do something else? And is this script really useful or are there any flaws I need to overcome? I am confused with these questions. Please help me...

Upvotes: 2

Views: 7438

Answers (2)

wowzers
wowzers

Reputation: 1

if ($_SERVER['HTTP_X_FORWARDED_FOR'])
  $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
else
  $ip   = $_SERVER['REMOTE_ADDR'];

$two_letter_country_code=iptocountry($ip);

function iptocountry($ip)
{   
  $numbers = explode( ".", $ip);

  include("ip_files/".$numbers[0].".php");
  $code=($numbers[0] * 16777216) + ($numbers[1] * 65536) + ($numbers[2] * 256) + ($numbers[3]);    

  foreach($ranges as $key => $value)
  {
    if($key<=$code)
    {
      if($ranges[$key][0]>=$code)
      {
        $country=$ranges[$key][1];break;
      }
    }

  }


  if ($country=="")
  {
    $country="unknown";
  }


error_reporting(E_ERROR | E_PARSE);

include ("mysqli_connection_file.php");
$query = "SELECT column_name FROM table_name";
$result = mysqli_query($conn, $query);

if (!$conn) { die("No Connection was made"); }

while($row = mysqli_fetch_assoc($result)) {

    $ccode = $row['name'];

}

    foreach ($ranges as $key => $value) {
      $countryset = $ranges[$key][1];
        //echo $countryset."<br />";

        if($ccode == $countryset) {
            $time = date('Y-m-d H:i');
            $insert = "INSERT IGNORE INTO logged_table VALUES ('','$ip','$time','IP Blocked','$countryset')";

                if ($conn->query($insert) === TRUE) {} else {
                    echo "Error: " . $sql . "<br>" . $conn->error;
                    die;
                }

            mysqli_free_result($result);
            mysqli_close($conn);


            echo "<script>alert('Please take a moment to review a very special offer we are running before you review!')</script>";
            echo "<META http-equiv='refresh' content='0; URL=http://www.leapfrog.com'>";

        }

    }
}

Upvotes: 0

Hasby Fahrudin
Hasby Fahrudin

Reputation: 50

In my expirience, it should work out by using in_array()

how about using common methode like if ($two_letter_country_code=="US" || $two_letter_country_code=="UK" ) is it working?

Upvotes: 2

Related Questions