php country dropdown by maxmind geoip

久未见 提交于 2019-12-12 02:58:09

问题


I would like to create a country drop down which can pre-select user country based on his/her Ip address. e.g., if user is in Italy, it must show Italy first while keeping all other countries in the list.

I searched a lot and I started by downloadin Maxmind GeoIP API and database. This is what I tried and it only shows a normal dropdown list without pre-select the country by ip:

  <select name="" multiple="multiple" width="200px" size="10px">
  <?php  

  require 'vendor/autoload.php'; //I put this is /var/www where my php file is  

  $gi = geoip_open('/usr/local/share/GeoIP/GeoIP.dat', GEOIP_STANDARD);
  $ip =  $_SERVER['REMOTE_ADDR'];
  echo $ip;
  $preselect_country = geoip_country_code_by_addr($gi, $ip);

  //newCountry.php is where I select all countries for drop down list
  include('newCountry.php');

  while ($line = mysql_fetch_array($result)) {

       if($preselect_country == $line){
         $selected = "selected";
     }else{
         $selected = "";
      }
  ?>

   <option value="<?php echo $line['country'];?>"<?php echo $selected; ?>><?php echo $line['country'];?> </option>;

  <?php
   }
  ?>
     geoip_close($gi);

 ?>
 </select>

I really tried my best to find the solution by myself, I read all these similar questions and tried also other solutions such as : Automatic Dropdown based on Country with Geoplugin, Fetching the current country name using ip address in php, Get Country of IP Address with PHP, Getting visitors country from their Ip and many more but I dont know why it does not work. I tried this code which worked, so I found I can get the ip:

 $ip =  $_SERVER['REMOTE_ADDR']; 
  echo $ip;

Also this sample worked for me (the output is ES Spain),

<?php

 require 'vendor/autoload.php';

 $gi = geoip_open("/usr/local/share/GeoIP/GeoIP.dat",GEOIP_STANDARD);

 echo geoip_country_code_by_addr($gi, "80.24.24.24") . "\t" .
 geoip_country_name_by_addr($gi, "80.24.24.24") . "\n";

 geoip_close($gi);

 ?>

but if I try exactly the same code by just substituting "80.24.24.24" with $ip, it returns nothing!!

#EDIT: Well, thanks to @vch, I found that problem is related to my ip since is for private network, so I got my real internet ip by ifconfig and used it in my code and it worked well. Till this point, I found that there is no problem with geoip api that I installed and also dropdown preselect works well.

This is the new code:

<select name = "question" class = "question" id = 'Question'> 
<?php

require 'vendor/autoload.php';

$gi = geoip_open('/usr/local/share/GeoIP/GeoIP.dat', GEOIP_STANDARD);
$ip = "131.175.122.222";

$preselect_country = geoip_country_name_by_addr($gi, $ip);  

include('newCountry.php');

while ($line = mysql_fetch_array($result)) {

   if($preselect_country == $line['country']){
       $selected = "selected";
   }else{
       $selected = "";
    }


 echo "<option value=\"{$line['country']}\" {$selected}>{$line['country']}</option>\n";
 }
   geoip_close($gi);

?>
</select>

Now my question is how to get real internet ip of users in case they used private networks like me?

All ideas are highly appreciated,

Thanks,


回答1:


try to get the IP using the below code, hope it will return you the exact IP.

$ip = getenv('HTTP_CLIENT_IP')?:
getenv('HTTP_X_FORWARDED_FOR')?:
getenv('HTTP_X_FORWARDED')?:
getenv('HTTP_FORWARDED_FOR')?:
getenv('HTTP_FORWARDED')?:
getenv('REMOTE_ADDR');


来源:https://stackoverflow.com/questions/25958564/php-country-dropdown-by-maxmind-geoip

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!