2016-11-17 235 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; 

此功能有内置的缓存功能很好,在我的测试,似乎非常快。