2016-11-17 31 views
0

我已經安裝了這個插件:https://tah.wordpress.org/plugins/geoip-detect/使用WordPress插件GeoIP檢測從IP獲取國家?

該插件似乎工作正常,當我在插件內測試「lookup」時,它返回我的地理信息。 但是,當我嘗試在我的一個wordpress頁面中實現代碼時,它不起作用。

$ip = $_SERVER['REMOTE_ADDR']; 
$userInfo = geoip_detect2_get_info_from_current_ip($ip); 
echo $userInfo->country->name; 

我從顯示單品的本地woocommerce頁面調用該函數。 但是這個函數什麼也沒有返回。 我是否需要添加更多功能才能調用功能geoip_detect2_get_info_from_current_ip()

我也試過:

<?php echo do_shortcode("[geoip_detect2 property='country']"); ?> 

它不返回任何東西。 我正在做godaddy的代碼編輯工具內的編輯,所以我可能會錯過錯誤。

回答

2

我已經嘗試實現由IP GEO在我的WordPress網站http://iradiofind.com/stations/。我正在使用http://www.geoplugin.net獲取國家/地區信息。爲了得到ip地址我使用這個功能

function get_client_ip() { 
    $ipaddress = ''; 
    if (getenv('HTTP_CLIENT_IP')) 
     $ipaddress = getenv('HTTP_CLIENT_IP'); 
    else if(getenv('HTTP_X_FORWARDED_FOR')) 
     $ipaddress = getenv('HTTP_X_FORWARDED_FOR'); 
    else if(getenv('HTTP_X_FORWARDED')) 
     $ipaddress = getenv('HTTP_X_FORWARDED'); 
    else if(getenv('HTTP_FORWARDED_FOR')) 
     $ipaddress = getenv('HTTP_FORWARDED_FOR'); 
    else if(getenv('HTTP_FORWARDED')) 
     $ipaddress = getenv('HTTP_FORWARDED'); 
    else if(getenv('REMOTE_ADDR')) 
     $ipaddress = getenv('REMOTE_ADDR'); 
    else 
     $ipaddress = 'UNKNOWN'; 

    return $ipaddress; 
} 

function ip_details($url) { 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 
    $data = curl_exec($ch); 
    curl_close($ch); 

    return $data; 
} 

這是在我的頁面模板。

<?php 
    $myipd = get_client_ip(); 
    $url = 'http://www.geoplugin.net/json.gp?ip='.$myipd; 
    $details = ip_details($url); 
    $v = json_decode($details); 
    $mycountry = $v->geoplugin_countryName; 
?> 
0

也許geoip-detect功能超出你的範圍(這將是非常奇怪的事情)。添加到您的的functions.php

require_once ABSPATH . '/wp-content/plugins/geoip-detect/geoip-detect.php'; 
0

我最近使用自定義解決方案相同的插件 - 我注意到插件內置有一個IP檢測功能 - geoip_detect2_get_client_ip() - 嘗試使用呢?

從您的代碼編輯:

$ip = geoip_detect2_get_client_ip(); 
$userInfo = geoip_detect2_get_info_from_current_ip($ip); 
echo $userInfo->country->name; 

此功能有內置的緩存功能很好,在我的測試,似乎非常快。