WMWW | Webmaster Forumu | Webmaster Platformu

Tam Versiyon: PHP Kodlarında Düzenleme Yaparak Ülke Koduna Göre Ziyaretçi IP Adresi Engelleme
Şu anda tam olmayan bir versiyonun içeriğine bakıyorsunuz. Tam versiyon'a bakınız.
Oluşturduğunuz projenize birçok sebepten ötürü, ülkelerden girişleri engellemek isteyebilirsiniz. Bu en doğal hakkınız. Fakat bu işlemi .htaccess üzerinden takip etmeyi denediğinizde, ülke bazlı IP class’larının sık sık değiştiği için zamanla işe yaramadığını görebilirsiniz. Bu da sizin IP bloklarını sık sık güncellemeniz anlamını taşır. Bunun yerine daha pratik bir çözüm geliştirdik. Aşağıdaki kod bütünü sitenizi ziyaret eden IP’nin anlık sorgusunu yaparak ülke koduna ulaşıyor ve sizin yasaklı listenizde ise, ziyaretçiye 403 Engel yanıtını gönderip çalışmayı durduruyor.

Aşağıdaki satırları, index.php veya ortak kullanılan (örn: config.php gibi) PHP dosyanızın en üstüne, <?php satırının hemen altına ekleyebilirsiniz:

PHP Kod:
// KOD BAŞLANGIÇ
function ip_info($ip NULL$purpose "location"$deep_detect TRUE) {
 
   $output NULL;
 
   if (filter_var($ipFILTER_VALIDATE_IP) === FALSE) {
 
       $ip $_SERVER["REMOTE_ADDR"];
 
       if ($deep_detect) {
 
           if (filter_var(@$_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP))
 
               $ip $_SERVER['HTTP_X_FORWARDED_FOR'];
 
           if (filter_var(@$_SERVER['HTTP_CLIENT_IP'], FILTER_VALIDATE_IP))
 
               $ip $_SERVER['HTTP_CLIENT_IP'];
 
       }
 
   }
 
   $purpose    str_replace(array("name""\n""\t"" ""-""_"), NULLstrtolower(trim($purpose)));
 
   $support    = array("country""countrycode""state""region""city""location""address");
 
   $continents = array(
 
       "AF" => "Africa",
 
       "AN" => "Antarctica",
 
       "AS" => "Asia",
 
       "EU" => "Europe",
 
       "OC" => "Australia (Oceania)",
 
       "NA" => "North America",
 
       "SA" => "South America"
 
   );
 
   if (filter_var($ipFILTER_VALIDATE_IP) && in_array($purpose$support)) {
 
       $ipdat = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=" $ip));
 
       if (@strlen(trim($ipdat->geoplugin_countryCode)) == 2) {
 
           switch ($purpose) {
 
               case "location":
 
                   $output = array(
 
                       "city"           => @$ipdat->geoplugin_city,
 
                       "state"          => @$ipdat->geoplugin_regionName,
 
                       "country"        => @$ipdat->geoplugin_countryName,
 
                       "country_code"   => @$ipdat->geoplugin_countryCode,
 
                       "continent"      => @$continents[strtoupper($ipdat->geoplugin_continentCode)],
 
                       "continent_code" => @$ipdat->geoplugin_continentCode
                    
);
 
                   break;
 
               case "address":
 
                   $address = array($ipdat->geoplugin_countryName);
 
                   if (@strlen($ipdat->geoplugin_regionName) >= 1)
 
                       $address[] = $ipdat->geoplugin_regionName;
 
                   if (@strlen($ipdat->geoplugin_city) >= 1)
 
                       $address[] = $ipdat->geoplugin_city;
 
                   $output implode(", "array_reverse($address));
 
                   break;
 
               case "city":
 
                   $output = @$ipdat->geoplugin_city;
 
                   break;
 
               case "state":
 
                   $output = @$ipdat->geoplugin_regionName;
 
                   break;
 
               case "region":
 
                   $output = @$ipdat->geoplugin_regionName;
 
                   break;
 
               case "country":
 
                   $output = @$ipdat->geoplugin_countryName;
 
                   break;
 
               case "countrycode":
 
                   $output = @$ipdat->geoplugin_countryCode;
 
                   break;
 
           }
 
       }
 
   }
 
   return $output;
}

// Engellenecek ülkelerin Country Kod'larını aşağıdaki array içine ekleyebilirsiniz
// 2 Harfli ISO kodlarını buradan bulabilirsiniz: https://www.nationsonline.org/oneworld/country_code_list.htm
$engelli_ulke_kodlari=array("RU"=>0"FR"=>0"CN"=>0"CO"=>0);

$ziyaretci_ulke_kodu ip_info("Visitor""Countrycode");
if (
array_key_exists($ziyaretci_ulke_kodu,$engelli_ulke_kodlari)){
 
 header('HTTP/1.1 403 Forbidden'); 
 
 echo "Forbidden Country IP! / Yasaklı Ülke IP'si";
 
 die();
}

// SON KOD BİTİŞİ 

Alıntı