Как определить город по IP адресу

Краткой и быстрое решение с использованием российской базы ipgeobase.ru

  1. <?php
  2. function GetRealIp() {
  3. if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
  4. $ip=$_SERVER['HTTP_CLIENT_IP'];
  5. } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  6. $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
  7. } else {
  8. $ip=$_SERVER['REMOTE_ADDR'];
  9. }
  10. return $ip;
  11. }
  12. $realUserIP = GetRealIp();
  13. ?>
  1. <em>Ваш город:</em> <span id="geocity"></span>
  2. <script>
  3. var ip = "<?php echo $realUserIP; ?>";
  4. jQuery.ajax ({
  5. type: "GET",
  6. url: "http://ipgeobase.ru:7020/geo/?ip=" + ip,
  7. dataType: "xml",
  8. success: function(xml) {
  9. var region = jQuery(xml).find('city').text();
  10. jQuery("#geocity").append(region);
  11. },
  12. error: function() {
  13. jQuery("#geocity").append("не определен");
  14. }
  15. });
  16. </script>
Russian
Категория: 
The code has been tested and works
Мультитег: 

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
Target Image