dongyunwei8596 2011-10-05 20:41 采纳率: 100%
浏览 23
已采纳

如何通过PHP检测中东访问者?

I'm looking for a clear cut way to detect if a visitor is from the Middle East. If they are, I need to display a different header image (the current one has a hog in it, so it is frowned upon).

I can code the if then else for that, but I'm just looking for a simple function to detect. Here's a function I used to get the country by IP:

function get_country_by_ip($ip){

    if(!$ip) return false;    # Missing parameter

    # Pull the XML
    $url = 'http://api.hostip.info/?ip='.$ip;
    $xml = simplexml_load_file($url);

    # Parse the data and store into array
    $citystate = explode(", ", $xml->children('gml', true)->featureMember->children()->Hostip->children('gml', true)->name);
    $result['city'] = $citystate[0];
    $result['state'] = $citystate[1];
    $result['country'] = $xml->children('gml', true)->featureMember->children()->Hostip->countryName;
    $result['country_abbr'] = $xml->children('gml', true)->featureMember->children()->Hostip->countryAbbrev;

    return (object) $result;

}

Can anyone help? Thanks.

  • 写回答

3条回答 默认 最新

  • dsk710351 2011-10-05 20:46
    关注

    You could define which countries fall into the "Middle East" group - by defining configurable array directly in the PHP file or in database and then check if the given country returned by your function is amongst the list of countries that belong to "Middle East". By your example, something like this:

    $middleEast = array(
     'Syria',
     'Iraq',
     ... // all the rest
    );
    
    $country = get_country_by_ip($ip);
    
    if (in_array($country['country'], $middleEast) {
     echo 'Middle East!';
     exit;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?