I would like to know on the site whether a visitor has come from a particular, or from any other country. I searched and found solutions to search on the global IP base.

But this is somehow too much, it seems to me. I don't need all the countries, I just need to know if a person is from a certain country or not.

Can I somehow do this with a little blood?

  • one
    If no joke: specify what information do you have? Why you do not like the option to search the world database and determine the "Ukraine / not Ukraine"? It may be worth digging in the direction of identifying a subset of IP from Ukraine and just storing this information. (However, I am not sure how often such information changes). And you will have users under TOR, who are actually from Ukraine, but they come through Tmutarakan. - Nick Volynkin
  • one
    you can use services like ipinfo.io and remember to cache requests. - KoVadim
  • 2
    Option to watch the request header "Accept-Language" for the presence of the Ukrainian language there. - Sergiks
  • Accept-Language, by the way, is suitable - thanks! I did not think about him. - anacol

2 answers 2

Downloading dumps of https://sypexgeo.net/ru/download/

you connect, and you can on any IP - find out any country and any city, and without connections during the determination to third-party sites!

    Here is an example of my implementation using wipmania

    function geoBlocking(){ if (!empty($_SERVER['HTTP_CLIENT_IP'])) { $ip = $_SERVER['HTTP_CLIENT_IP']; } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; } else { $ip = $_SERVER['REMOTE_ADDR']; } $server = $_SERVER['HTTP_HOST']; $country = file_get_contents('http://api.wipmania.com/'.$ip.'?'.$server.''); $validate = 'US'; if($validate !== $country){ //code } else { header($_SERVER['SERVER_PROTOCOL'].' 400 Bad Request'); exit('you country is blocked'); } } 
    • one
      Perfect solution. Only I think that it is necessary to do 20-30 checks in a loop to several services in order for sure. - Athari
    • Well, I made 20 requests, everyone said a different result. So, what is next? Plus there are disputed territories, how to define them? - KoVadim
    • @KoVadim: Need a separate table of addresses of the disputed territories, of course. If the service does not provide them - this is a bad service. - VladD
    • the service may not consider the disputed territory as such. Moreover, aypishniki can migrate and my friends got into this situation. (a large company moved part of its ip from one country to another). As a result, they were banned in Google (yes, it happens). But there is an even better situation. On my phone, when I go to the site of my operator through 3g, the area is incorrectly determined. Conclusion - it is necessary to calculate by ip carefully. - KoVadim