0

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,

Community
  • 1
  • 1
mOna
  • 2,341
  • 9
  • 36
  • 60
  • First, have tried turning on [error reporting](http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php)? That's always a good first step. – vch Sep 21 '14 at 11:16
  • @vch, I just checked the error.log , I am reading the link u suggested. I will try it – mOna Sep 21 '14 at 11:27
  • @vch: I don't receive any error in error.log, I think my problem is related to this ip : 10.48.44.43 (this is the ip that i get by running $_SERVER['REMOTE_ADDR'];) , when I enter this Ip in any of the websites which get ip and returnes country, it returns nothing even in those sites!! instead they show me another ip! I am really confused :( – mOna Sep 21 '14 at 15:10
  • Are you trying to use this on a local network vs. over the Internet? That IP range looks like a private network. – vch Sep 21 '14 at 18:19
  • @vch: well, I think yes since I connect to my university server by openvpn.. what should i do know? how can i get real ip? – mOna Sep 21 '14 at 22:49
  • @vch: tnx a lot for your point:)I tested with my real ip and it works!! now my question is how to get real ip of users in case if they used e.g vpn like me?? – mOna Sep 22 '14 at 00:14

1 Answers1

0

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');
Ram Sharma
  • 8,676
  • 7
  • 43
  • 56