我想創建一個國家下拉菜單,可以根據他/她的IP地址預先選擇用戶國家。例如,如果用戶在意大利,它必須首先顯示意大利,同時將所有其他國家列入清單。php國家下拉由maxmind geoip
我搜索了很多,我開始下載Maxmind GeoIP API和數據庫。這就是我想和那隻能說明沒有預先選擇通過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>
我真的盡力了我自己想辦法了,我讀了所有這些類似的問題,也嘗試過其他解決方案,如: 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等等,但我不知道爲什麼它不起作用。 我想這個代碼的工作,所以我發現我可以得到IP:
$ip = $_SERVER['REMOTE_ADDR'];
echo $ip;
而且這個樣品爲我工作(輸出ES西班牙),
<?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」即可得到相同的代碼,但它不會返回任何結果!
#EDIT: 好,感謝@vch,我發現因爲是專用網絡問題涉及到我的IP,所以我使用ifconfig得到了我真正的互聯網IP,在我的代碼中使用它和它的工作好。直到這一點,我發現我安裝的geoip api沒有問題,並且下拉預選效果也很好。
這是新代碼:
<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>
現在的問題是如何讓用戶真正的Internet IP在他們使用專用網絡像我這樣的情況?
所有的想法都高度讚賞,
感謝,
首先,嘗試開啓[錯誤報告](http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php)?這總是第一步。 – vch 2014-09-21 11:16:35
@vch,我剛剛檢查了error.log,我正在閱讀你建議的鏈接。我會試試 – mOna 2014-09-21 11:27:59
@vch:我在error.log中沒有收到任何錯誤,我想我的問題與此ip相關:10.48.44.43(這是我通過運行$ _SERVER ['REMOTE_ADDR']獲得的IP ;),當我在任何獲得ip和返回國家的網站上輸入這個IP時,即使在這些網站上它也不會返回任何內容!相反,他們告訴我另一個IP!我真的很困惑:( – mOna 2014-09-21 15:10:20