Forumcu
Forum Üyesii
- Katılım
- 23 Eyl 2022
- Mesajlar
- 1,232
- Puanları
- 36
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:
Alıntı
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:
[COLOR=#FF8000]// KOD BAŞLANGIÇ[/COLOR]
[COLOR=#007700]function [/COLOR][COLOR=#0000BB]ip_info[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]$ip [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000BB]NULL[/COLOR][COLOR=#007700], [/COLOR][COLOR=#0000BB]$purpose [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#DD0000]"location"[/COLOR][COLOR=#007700], [/COLOR][COLOR=#0000BB]$deep_detect [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000BB]TRUE[/COLOR][COLOR=#007700]) {
[/COLOR][COLOR=#0000BB] $output [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000BB]NULL[/COLOR][COLOR=#007700];
[/COLOR][COLOR=#0000BB] if [/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]filter_var[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]$ip[/COLOR][COLOR=#007700], [/COLOR][COLOR=#0000BB]FILTER_VALIDATE_IP[/COLOR][COLOR=#007700]) === [/COLOR][COLOR=#0000BB]FALSE[/COLOR][COLOR=#007700]) {
[/COLOR][COLOR=#0000BB] $ip [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000BB]$_SERVER[/COLOR][COLOR=#007700][[/COLOR][COLOR=#DD0000]"REMOTE_ADDR"[/COLOR][COLOR=#007700]];
[/COLOR][COLOR=#0000BB] if [/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]$deep_detect[/COLOR][COLOR=#007700]) {
[/COLOR][COLOR=#0000BB] if [/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]filter_var[/COLOR][COLOR=#007700](@[/COLOR][COLOR=#0000BB]$_SERVER[/COLOR][COLOR=#007700][[/COLOR][COLOR=#DD0000]'HTTP_X_FORWARDED_FOR'[/COLOR][COLOR=#007700]], [/COLOR][COLOR=#0000BB]FILTER_VALIDATE_IP[/COLOR][COLOR=#007700]))
[/COLOR][COLOR=#0000BB] $ip [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000BB]$_SERVER[/COLOR][COLOR=#007700][[/COLOR][COLOR=#DD0000]'HTTP_X_FORWARDED_FOR'[/COLOR][COLOR=#007700]];
[/COLOR][COLOR=#0000BB] if [/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]filter_var[/COLOR][COLOR=#007700](@[/COLOR][COLOR=#0000BB]$_SERVER[/COLOR][COLOR=#007700][[/COLOR][COLOR=#DD0000]'HTTP_CLIENT_IP'[/COLOR][COLOR=#007700]], [/COLOR][COLOR=#0000BB]FILTER_VALIDATE_IP[/COLOR][COLOR=#007700]))
[/COLOR][COLOR=#0000BB] $ip [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000BB]$_SERVER[/COLOR][COLOR=#007700][[/COLOR][COLOR=#DD0000]'HTTP_CLIENT_IP'[/COLOR][COLOR=#007700]];
[/COLOR][COLOR=#0000BB] [/COLOR][COLOR=#007700]}
[/COLOR][COLOR=#0000BB] [/COLOR][COLOR=#007700]}
[/COLOR][COLOR=#0000BB] $purpose [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000BB]str_replace[/COLOR][COLOR=#007700](array([/COLOR][COLOR=#DD0000]"name"[/COLOR][COLOR=#007700], [/COLOR][COLOR=#DD0000]"\n"[/COLOR][COLOR=#007700], [/COLOR][COLOR=#DD0000]"\t"[/COLOR][COLOR=#007700], [/COLOR][COLOR=#DD0000]" "[/COLOR][COLOR=#007700], [/COLOR][COLOR=#DD0000]"-"[/COLOR][COLOR=#007700], [/COLOR][COLOR=#DD0000]"_"[/COLOR][COLOR=#007700]), [/COLOR][COLOR=#0000BB]NULL[/COLOR][COLOR=#007700], [/COLOR][COLOR=#0000BB]strtolower[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]trim[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]$purpose[/COLOR][COLOR=#007700])));
[/COLOR][COLOR=#0000BB] $support [/COLOR][COLOR=#007700]= array([/COLOR][COLOR=#DD0000]"country"[/COLOR][COLOR=#007700], [/COLOR][COLOR=#DD0000]"countrycode"[/COLOR][COLOR=#007700], [/COLOR][COLOR=#DD0000]"state"[/COLOR][COLOR=#007700], [/COLOR][COLOR=#DD0000]"region"[/COLOR][COLOR=#007700], [/COLOR][COLOR=#DD0000]"city"[/COLOR][COLOR=#007700], [/COLOR][COLOR=#DD0000]"location"[/COLOR][COLOR=#007700], [/COLOR][COLOR=#DD0000]"address"[/COLOR][COLOR=#007700]);
[/COLOR][COLOR=#0000BB] $continents [/COLOR][COLOR=#007700]= array(
[/COLOR][COLOR=#0000BB] [/COLOR][COLOR=#DD0000]"AF" [/COLOR][COLOR=#007700]=> [/COLOR][COLOR=#DD0000]"Africa"[/COLOR][COLOR=#007700],
[/COLOR][COLOR=#0000BB] [/COLOR][COLOR=#DD0000]"AN" [/COLOR][COLOR=#007700]=> [/COLOR][COLOR=#DD0000]"Antarctica"[/COLOR][COLOR=#007700],
[/COLOR][COLOR=#0000BB] [/COLOR][COLOR=#DD0000]"AS" [/COLOR][COLOR=#007700]=> [/COLOR][COLOR=#DD0000]"Asia"[/COLOR][COLOR=#007700],
[/COLOR][COLOR=#0000BB] [/COLOR][COLOR=#DD0000]"EU" [/COLOR][COLOR=#007700]=> [/COLOR][COLOR=#DD0000]"Europe"[/COLOR][COLOR=#007700],
[/COLOR][COLOR=#0000BB] [/COLOR][COLOR=#DD0000]"OC" [/COLOR][COLOR=#007700]=> [/COLOR][COLOR=#DD0000]"Australia (Oceania)"[/COLOR][COLOR=#007700],
[/COLOR][COLOR=#0000BB] [/COLOR][COLOR=#DD0000]"NA" [/COLOR][COLOR=#007700]=> [/COLOR][COLOR=#DD0000]"North America"[/COLOR][COLOR=#007700],
[/COLOR][COLOR=#0000BB] [/COLOR][COLOR=#DD0000]"SA" [/COLOR][COLOR=#007700]=> [/COLOR][COLOR=#DD0000]"South America"
[/COLOR][COLOR=#0000BB] [/COLOR][COLOR=#007700]);
[/COLOR][COLOR=#0000BB] if [/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]filter_var[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]$ip[/COLOR][COLOR=#007700], [/COLOR][COLOR=#0000BB]FILTER_VALIDATE_IP[/COLOR][COLOR=#007700]) && [/COLOR][COLOR=#0000BB]in_array[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]$purpose[/COLOR][COLOR=#007700], [/COLOR][COLOR=#0000BB]$support[/COLOR][COLOR=#007700])) {
[/COLOR][COLOR=#0000BB] $ipdat [/COLOR][COLOR=#007700]= @[/COLOR][COLOR=#0000BB]json_decode[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]file_get_contents[/COLOR][COLOR=#007700]([/COLOR][COLOR=#DD0000]"http://www.geoplugin.net/json.gp?ip=" [/COLOR][COLOR=#007700]. [/COLOR][COLOR=#0000BB]$ip[/COLOR][COLOR=#007700]));
[/COLOR][COLOR=#0000BB] if [/COLOR][COLOR=#007700](@[/COLOR][COLOR=#0000BB]strlen[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]trim[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]$ipdat[/COLOR][COLOR=#007700]->[/COLOR][COLOR=#0000BB]geoplugin_countryCode[/COLOR][COLOR=#007700])) == [/COLOR][COLOR=#0000BB]2[/COLOR][COLOR=#007700]) {
[/COLOR][COLOR=#0000BB] switch [/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]$purpose[/COLOR][COLOR=#007700]) {
[/COLOR][COLOR=#0000BB] case [/COLOR][COLOR=#DD0000]"location"[/COLOR][COLOR=#007700]:
[/COLOR][COLOR=#0000BB] $output [/COLOR][COLOR=#007700]= array(
[/COLOR][COLOR=#0000BB] [/COLOR][COLOR=#DD0000]"city" [/COLOR][COLOR=#0000BB] [/COLOR][COLOR=#007700]=> @[/COLOR][COLOR=#0000BB]$ipdat[/COLOR][COLOR=#007700]->[/COLOR][COLOR=#0000BB]geoplugin_city[/COLOR][COLOR=#007700],
[/COLOR][COLOR=#0000BB] [/COLOR][COLOR=#DD0000]"state" [/COLOR][COLOR=#0000BB] [/COLOR][COLOR=#007700]=> @[/COLOR][COLOR=#0000BB]$ipdat[/COLOR][COLOR=#007700]->[/COLOR][COLOR=#0000BB]geoplugin_regionName[/COLOR][COLOR=#007700],
[/COLOR][COLOR=#0000BB] [/COLOR][COLOR=#DD0000]"country" [/COLOR][COLOR=#0000BB] [/COLOR][COLOR=#007700]=> @[/COLOR][COLOR=#0000BB]$ipdat[/COLOR][COLOR=#007700]->[/COLOR][COLOR=#0000BB]geoplugin_countryName[/COLOR][COLOR=#007700],
[/COLOR][COLOR=#0000BB] [/COLOR][COLOR=#DD0000]"country_code" [/COLOR][COLOR=#0000BB] [/COLOR][COLOR=#007700]=> @[/COLOR][COLOR=#0000BB]$ipdat[/COLOR][COLOR=#007700]->[/COLOR][COLOR=#0000BB]geoplugin_countryCode[/COLOR][COLOR=#007700],
[/COLOR][COLOR=#0000BB] [/COLOR][COLOR=#DD0000]"continent" [/COLOR][COLOR=#0000BB] [/COLOR][COLOR=#007700]=> @[/COLOR][COLOR=#0000BB]$continents[/COLOR][COLOR=#007700][[/COLOR][COLOR=#0000BB]strtoupper[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]$ipdat[/COLOR][COLOR=#007700]->[/COLOR][COLOR=#0000BB]geoplugin_continentCode[/COLOR][COLOR=#007700])],
[/COLOR][COLOR=#0000BB] [/COLOR][COLOR=#DD0000]"continent_code" [/COLOR][COLOR=#007700]=> @[/COLOR][COLOR=#0000BB]$ipdat[/COLOR][COLOR=#007700]->[/COLOR][COLOR=#0000BB]geoplugin_continentCode
[/COLOR][COLOR=#007700]);
[/COLOR][COLOR=#0000BB] break[/COLOR][COLOR=#007700];
[/COLOR][COLOR=#0000BB] case [/COLOR][COLOR=#DD0000]"address"[/COLOR][COLOR=#007700]:
[/COLOR][COLOR=#0000BB] $address [/COLOR][COLOR=#007700]= array([/COLOR][COLOR=#0000BB]$ipdat[/COLOR][COLOR=#007700]->[/COLOR][COLOR=#0000BB]geoplugin_countryName[/COLOR][COLOR=#007700]);
[/COLOR][COLOR=#0000BB] if [/COLOR][COLOR=#007700](@[/COLOR][COLOR=#0000BB]strlen[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]$ipdat[/COLOR][COLOR=#007700]->[/COLOR][COLOR=#0000BB]geoplugin_regionName[/COLOR][COLOR=#007700]) >= [/COLOR][COLOR=#0000BB]1[/COLOR][COLOR=#007700])
[/COLOR][COLOR=#0000BB] $address[/COLOR][COLOR=#007700][] = [/COLOR][COLOR=#0000BB]$ipdat[/COLOR][COLOR=#007700]->[/COLOR][COLOR=#0000BB]geoplugin_regionName[/COLOR][COLOR=#007700];
[/COLOR][COLOR=#0000BB] if [/COLOR][COLOR=#007700](@[/COLOR][COLOR=#0000BB]strlen[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]$ipdat[/COLOR][COLOR=#007700]->[/COLOR][COLOR=#0000BB]geoplugin_city[/COLOR][COLOR=#007700]) >= [/COLOR][COLOR=#0000BB]1[/COLOR][COLOR=#007700])
[/COLOR][COLOR=#0000BB] $address[/COLOR][COLOR=#007700][] = [/COLOR][COLOR=#0000BB]$ipdat[/COLOR][COLOR=#007700]->[/COLOR][COLOR=#0000BB]geoplugin_city[/COLOR][COLOR=#007700];
[/COLOR][COLOR=#0000BB] $output [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000BB]implode[/COLOR][COLOR=#007700]([/COLOR][COLOR=#DD0000]", "[/COLOR][COLOR=#007700], [/COLOR][COLOR=#0000BB]array_reverse[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]$address[/COLOR][COLOR=#007700]));
[/COLOR][COLOR=#0000BB] break[/COLOR][COLOR=#007700];
[/COLOR][COLOR=#0000BB] case [/COLOR][COLOR=#DD0000]"city"[/COLOR][COLOR=#007700]:
[/COLOR][COLOR=#0000BB] $output [/COLOR][COLOR=#007700]= @[/COLOR][COLOR=#0000BB]$ipdat[/COLOR][COLOR=#007700]->[/COLOR][COLOR=#0000BB]geoplugin_city[/COLOR][COLOR=#007700];
[/COLOR][COLOR=#0000BB] break[/COLOR][COLOR=#007700];
[/COLOR][COLOR=#0000BB] case [/COLOR][COLOR=#DD0000]"state"[/COLOR][COLOR=#007700]:
[/COLOR][COLOR=#0000BB] $output [/COLOR][COLOR=#007700]= @[/COLOR][COLOR=#0000BB]$ipdat[/COLOR][COLOR=#007700]->[/COLOR][COLOR=#0000BB]geoplugin_regionName[/COLOR][COLOR=#007700];
[/COLOR][COLOR=#0000BB] break[/COLOR][COLOR=#007700];
[/COLOR][COLOR=#0000BB] case [/COLOR][COLOR=#DD0000]"region"[/COLOR][COLOR=#007700]:
[/COLOR][COLOR=#0000BB] $output [/COLOR][COLOR=#007700]= @[/COLOR][COLOR=#0000BB]$ipdat[/COLOR][COLOR=#007700]->[/COLOR][COLOR=#0000BB]geoplugin_regionName[/COLOR][COLOR=#007700];
[/COLOR][COLOR=#0000BB] break[/COLOR][COLOR=#007700];
[/COLOR][COLOR=#0000BB] case [/COLOR][COLOR=#DD0000]"country"[/COLOR][COLOR=#007700]:
[/COLOR][COLOR=#0000BB] $output [/COLOR][COLOR=#007700]= @[/COLOR][COLOR=#0000BB]$ipdat[/COLOR][COLOR=#007700]->[/COLOR][COLOR=#0000BB]geoplugin_countryName[/COLOR][COLOR=#007700];
[/COLOR][COLOR=#0000BB] break[/COLOR][COLOR=#007700];
[/COLOR][COLOR=#0000BB] case [/COLOR][COLOR=#DD0000]"countrycode"[/COLOR][COLOR=#007700]:
[/COLOR][COLOR=#0000BB] $output [/COLOR][COLOR=#007700]= @[/COLOR][COLOR=#0000BB]$ipdat[/COLOR][COLOR=#007700]->[/COLOR][COLOR=#0000BB]geoplugin_countryCode[/COLOR][COLOR=#007700];
[/COLOR][COLOR=#0000BB] break[/COLOR][COLOR=#007700];
[/COLOR][COLOR=#0000BB] [/COLOR][COLOR=#007700]}
[/COLOR][COLOR=#0000BB] [/COLOR][COLOR=#007700]}
[/COLOR][COLOR=#0000BB] [/COLOR][COLOR=#007700]}
[/COLOR][COLOR=#0000BB] return $output[/COLOR][COLOR=#007700];
}
[/COLOR]
[COLOR=#FF8000]// 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[/COLOR]
[COLOR=#0000BB]$engelli_ulke_kodlari[/COLOR][COLOR=#007700]=array([/COLOR][COLOR=#DD0000]"RU"[/COLOR][COLOR=#007700]=>[/COLOR][COLOR=#0000BB]0[/COLOR][COLOR=#007700], [/COLOR][COLOR=#DD0000]"FR"[/COLOR][COLOR=#007700]=>[/COLOR][COLOR=#0000BB]0[/COLOR][COLOR=#007700], [/COLOR][COLOR=#DD0000]"CN"[/COLOR][COLOR=#007700]=>[/COLOR][COLOR=#0000BB]0[/COLOR][COLOR=#007700], [/COLOR][COLOR=#DD0000]"CO"[/COLOR][COLOR=#007700]=>[/COLOR][COLOR=#0000BB]0[/COLOR][COLOR=#007700]);
[/COLOR]
[COLOR=#0000BB]$ziyaretci_ulke_kodu [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000BB]ip_info[/COLOR][COLOR=#007700]([/COLOR][COLOR=#DD0000]"Visitor"[/COLOR][COLOR=#007700], [/COLOR][COLOR=#DD0000]"Countrycode"[/COLOR][COLOR=#007700]);
if ([/COLOR][COLOR=#0000BB]array_key_exists[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]$ziyaretci_ulke_kodu[/COLOR][COLOR=#007700],[/COLOR][COLOR=#0000BB]$engelli_ulke_kodlari[/COLOR][COLOR=#007700])){
[/COLOR][COLOR=#0000BB] header[/COLOR][COLOR=#007700]([/COLOR][COLOR=#DD0000]'HTTP/1.1 403 Forbidden'[/COLOR][COLOR=#007700]);
[/COLOR][COLOR=#0000BB] echo [/COLOR][COLOR=#DD0000]"Forbidden Country IP! / Yasaklı Ülke IP'si"[/COLOR][COLOR=#007700];
[/COLOR][COLOR=#0000BB] die[/COLOR][COLOR=#007700]();
}
[/COLOR]
[COLOR=#FF8000]// SON KOD BİTİŞİ [/COLOR]
Alıntı