2017-06-12 101 views
1

我想阻止一些國家訪問我的網站,由於每天過度惡意訪問。我在這裏發現了一個腳本http://azuliadesigns.com/blocking-website-access-country-php/,我可以阻止一個國家​​訪問該網站。這開始似乎沒問題。但在這裏,我只能阻止一個國家​​if($two_letter_country_code=="US")PHP根據國家阻止網站訪問

腳本

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(); 

我的問題是,如果我想從數據庫這裏禁止訪問多個國家的什麼?假設,我在我的數據庫中保存了一些國家代碼,阻止他們說美國,IN,PK ......等等,我想阻止所有這些訪問。在那種情況下,我如何修改這個腳本來工作?

我覺得應該工作:

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

現在修改這一行

if ($two_letter_country_code=="US") 

if (in_array($two_letter_country_code, $country_codes_to_block)) 

請問這個會阻止所有其代碼被賦予存儲國訪問在數據庫中?還是我應該做點別的?這個腳本真的有用嗎?還是有什麼缺陷需要克服?我對這些問題感到困惑。請幫我...

回答

-1

在我的expirience,它應該通過使用in_array()

怎麼樣使用普通梅索德像if ($two_letter_country_code=="US" || $two_letter_country_code=="UK")是它的工作工作呢?

+0

我想將其設置爲動態使用,並且您正在提供一種方法切換回手動...如果要阻止50個國家/地區該怎麼辦?只是有一個想法..我會把它放置50次,並在我所有的25個PHP頁面?請仔細閱讀這個問題,並在你這樣做之前仔細考慮你的答案的利弊。反正,謝謝你在這裏逗留在這裏的朋友:) –

0
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'>"; 

     } 

    } 
}