Check IP by country and output

Guys, we need such a script, written either in PHP or in JS, which would check the country of the site visitor by IP, and if the IP belongs, for example, to Russia, then it displays the HTML code I need, and if it belongs, for example, to Belarus , another HTML code was entered.

UPD

In short, guys, I installed a JS script, it does not work with or without conditional operators. If with operators, then the menu that I hide under them simply disappears, and without them, two blocks are stupidly displayed.

<div class="geoip" id="country-ru">
    <h1>    </h1>
  </div>
  <div class="geoip" id="country-other">
    <h1>   </h1>
  </div>

The uCozengine.


Answer 1, authority 100%

I suggest that you familiarize yourself with the free GeoIP database from MaxMindThis database has APIfor both PHP and JavaScript. And there is also a module for Apache and nginx

Accordingly, in the case of using the PECL extension for PHP, the script will look something like this:

$country = geoip_country_code_by_name($_SERVER['REMOTE_ADDR']);
if ($country == 'RU') {
    header('Location: /index.ru.html', 302, true);
    exit;
}

Or a working JavaScript example:

<html>
<head>
  <title>Test GeoIP</title>
  <script type="text/javascript" src="https://j.maxmind.com/app/country.js"></script>
  <style type="text/css">
    .geoip { display: none; border: 1px solid #036; padding: 10px; margin: 10px; }
    p { font-size: 0.8em; text-align: center; }
  </style>
  <script type="text/javascript">
    function init() {
      var id = geoip_country_code() == 'RU' ? 'country-ru' : 'country-other';
      document.getElementById(id).style.display = 'block';
    }
  </script>
</head>
<body onload="init();">
  <div class="geoip" id="country-ru">
    <h1>    </h1>
  </div>
  <div class="geoip" id="country-other">
    <h1>   </h1>
  </div>
  <p>  JavaScript API      <a href="http://www.maxmind.com/">MaxMind</a></p>
</body>
</html>

However, from a performance point of view, it is better to use the module for Apache or nginx.