由maxmind geoip提供的php国家/地区下拉列表 [英] php country dropdown by maxmind geoip

查看:197
本文介绍了由maxmind geoip提供的php国家/地区下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个国家/地区下拉菜单,可以根据他/她的IP地址预先选择用户所在国家/地区。例如,如果用户在意大利,它必须首先显示意大利,同时保留列表中的所有其他国家。

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.

我搜索了很多,我开始通过下载Maxmind GeoIP API和数据库。这是我尝试的,它只显示一个正常的下拉列表而没有通过ip预选国家:

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>

我真的尽力找到自己的解决方案,我读了所有这些类似的问题并试过其他解决方案,例如:
基于自动下拉在具有Geoplugin的国家/地区获取当前国家/地区名称使用php中的IP地址获取IP地址所在的国家/地区PHP 让访客来自他们的国家/地区还有更多,但我不知道为什么它不起作用。
我尝试了这个有效的代码,所以我发现我可以获得ip:

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;

此示例也适用于我(输出为ES西班牙),

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);

 ?>

但是如果我用$ ip替换80.24.24.24来尝试完全相同的代码,那么没有返回!!

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

#EDIT:
好​​吧,多亏了@vch,我发现问题与我的ip有关是专用网络,所以我通过ifconfig得到了我真正的互联网IP并在我的代码中使用它并且运行良好。直到这一点,我发现我安装的geoip api没有问题,并且预删除预选效果也很好。

# 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.

这是新代码:

<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>

现在我的问题是如何使用像我这样的私人网络来获取用户的真实互联网IP?

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,

谢谢,

推荐答案

尝试使用以下代码获取IP,希望它能为您返回确切的IP。

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');

这篇关于由maxmind geoip提供的php国家/地区下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆