【问题标题】:PHP Geo LocationPHP 地理位置
【发布时间】:2019-04-11 11:00:04
【问题描述】:

您好,我的 php 代码似乎有点意外。下面的if($location !== "AU"){ 条件上周有效,但现在不行。想知道 ipinfo 服务是否不稳定?我目前在 AU,我不希望它显示。 有什么帮助吗?

<?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>';
    }
    }
?>

【问题讨论】:

  • 您可能已达到每天 1000 个请求的免费计划限制?您应该注册一个访问令牌并将其添加到您的请求中,以便获得速率限制通知。

标签: php wordpress geolocation


【解决方案1】:

这似乎解决了我的问题

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"){
...

【讨论】:

    猜你喜欢
    • 2011-04-06
    • 2012-11-16
    • 2013-08-19
    • 1970-01-01
    • 2016-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多