PHP地理位置

时间:2018-11-07 23:01:43

标签: php wordpress geolocation

您好,我的php代码似乎有点偶然。符合以下条件的if($location !== "AU"){上周可以正常工作,但现在没有。想知道ipinfo服务是否不稳定?我目前在非盟,但我不想显示它。 有什么帮助吗?

<?php
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
$details = json_decode(file_get_contents("http://ipinfo.io/{$ip}/json"));
$location = $details->country;
if($location !== "AU"){
    if(is_page(array('industrial-hoses', 'industrial-clamps', 'industrial- 
    adaptors', 'rubber-industrial-hoses', 'industrial-couplings', 'industrial- 
    accessories', 'industrial-hose-rubber-air', 'rubber-air-water-industrial-hose', 
    'rubber-chemical-acid-industrial-hose', 'rubber-food-beverage-industrial-hose',  
    'rubber-fuel-oil-industrial-hose', 'rubber-material-handling-industrial-hose', 
    'rubber-multi-purpose-industrial-hose' ,'rubber-specialty-industrial-hose', 
    'pvc-industrial-hoses', 'pvc-air-water-industrial-hose', 'pvc-chemical-acid- 
    industrial-hose', 'pvc-food-beverage-industrial-hose', 'pvc-fuel-oil- 
    industrial-hose', 'pvc-material-handling-industrial-hose', 'pvc-multi-purpose- 
    industrial-hose', 'pvc-speciality-industrial-hose', 'teflon-speciality- 
    industrial-hose', 'flexible-ducting-speciality-industrial-hose'))){
        echo '<script src="https://website.com/f/embed.php?id=7" type="text/javascript" charset="utf-8"></script>';
    }
    }
?>

1 个答案:

答案 0 :(得分:0)

这似乎解决了我的问题

function getClientIP(){
    if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
    $ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
    $ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}

$ipaddress = getClientIP();

function ip_details($ip) {
  $json = file_get_contents("http://ipinfo.io/{$ip}/geo");
  $details = json_decode($json, true);
  return $details;
}

$details = ip_details($ipaddress);

if($details['country'] === "AU"){
...