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

UPD

In short, guys, put the JS script, does not work with conditional operators or without them. If with the operators, the menu that I hide under them just 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> 

uCoz engine.

    1 answer 1

    I suggest to get acquainted with the free GeoIP database from MaxMind . This database has an API for both PHP and JavaScript. And there is also a module for Apache and nginx.

    Accordingly, in the case of using PECL extensions 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="http://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, in terms of performance, it is better to use the module for Apache or nginx.

    • Oh, thanks for the code. It's just that I myself am in PHP nubik, now I will try to do it, but I think it will have to be done to output not just the page, but just the HTML code. And can I talk to you personally? - Eugene Shilin
    • Now I tried, but it did not work, because in PHP, in general, oak is oak ... Ilya or someone else help in drawing up the script? - Eugene Shilin
    • one
      You must first install the appropriate [PECL extension] [1]. Or use the JavaScript API. > And can I talk to you personally? If with the purpose of consulting in PHP, then it is not necessary =) [1]: php.net/manual/en/geoip.installation.php - Ilya Pirogov
    • Js just by the way) Truth and there I am a nubik) Many thanks for the info. Tell me, will it work like this? <body onload = "init ();"> <script type = "text / javascript"> if (country-ru) {Block for users from Russia} else {something else} </ script> </ body> Or what condition to register? - Eugene Shilin
    • if (geoip_country_code () == 'RU') {} [JavaScript Introduction] [ w3schools.com/js/js_intro.asp] - Ilya Pirogov