如何通过 PHP 检测中东访客?

发布于 2024-12-08 12:34:59 字数 916 浏览 0 评论 0原文

我正在寻找一种明确的方法来检测访客是否来自中东。如果是的话,我需要显示一个不同的标题图像(当前的标题图像中有一只猪,所以它不受欢迎)。

我可以为此编写 if then else 代码,但我只是在寻找一个简单的函数来检测。这是我用来通过 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;

}

有人可以帮忙吗?谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

往日情怀 2024-12-15 12:34:59

您可以定义哪些国家/地区属于“中东”组 - 通过直接在 PHP 文件或数据库中定义可配置数组,然后检查函数返回的给定国家/地区是否在属于“中东”的国家/地区列表中。以你的例子来说,是这样的:

$middleEast = array(
 'Syria',
 'Iraq',
 ... // all the rest
);

$country = get_country_by_ip($ip);

if (in_array($country['country'], $middleEast) {
 echo 'Middle East!';
 exit;
}

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;
}
银河中√捞星星 2024-12-15 12:34:59
<?php

/* get country by ip */

function get_country_by_ip($ip){

    if(!$ip) return false;

    /* 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'] = (array) $xml->children('gml', true)->featureMember->children()->Hostip->countryName;
    $result['country'] = $result['country'][0];

    $result['country_abbr'] = (array) $xml->children('gml', true)->featureMember->children()->Hostip->countryAbbrev;
    $result['country_abbr'] = $result['country_abbr'][0];

    return (object) $result;

}


/* get country */

$geo_info = get_country_by_ip($_SERVER['REMOTE_ADDR']);


/* MENA countries */

$mena = array(

    'ALGERIA',

    'BAHRAIN',

    'EGYPT',

    'IRAN',

    'IRAQ',

    'ISRAEL',

    'JORDAN',

    'KUWAIT',

    'LEBANON',

    'LIBYA',

    'MOROCCO',

    'OMAN',

    'PALESTINE',

    'QATAR',

    'SAUDI ARABIA',

    'SYRIA',

    'TUNISIA',

    'UNITED ARAB EMIRATES',

    'YEMEN',

    'ARMENIA',

    'AZERBAIJAN',

    'CYPRUS',

    'DJIBOUTI',

    'MALTA',

    'MAURITANIA',

    'SAHRAWI ARAB DEMOCRATIC REPUBLIC',

    'SOMALIA',

    'SUDAN',

    'TURKEY',

);


/* image with a hog */

$img = 'mmm-pork.jpg';

if(in_array($geo_info->country, $mena)){


    /* image with no hog */

    $img = 'cant-have-pork.jpg';

} ?>
<?php

/* get country by ip */

function get_country_by_ip($ip){

    if(!$ip) return false;

    /* 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'] = (array) $xml->children('gml', true)->featureMember->children()->Hostip->countryName;
    $result['country'] = $result['country'][0];

    $result['country_abbr'] = (array) $xml->children('gml', true)->featureMember->children()->Hostip->countryAbbrev;
    $result['country_abbr'] = $result['country_abbr'][0];

    return (object) $result;

}


/* get country */

$geo_info = get_country_by_ip($_SERVER['REMOTE_ADDR']);


/* MENA countries */

$mena = array(

    'ALGERIA',

    'BAHRAIN',

    'EGYPT',

    'IRAN',

    'IRAQ',

    'ISRAEL',

    'JORDAN',

    'KUWAIT',

    'LEBANON',

    'LIBYA',

    'MOROCCO',

    'OMAN',

    'PALESTINE',

    'QATAR',

    'SAUDI ARABIA',

    'SYRIA',

    'TUNISIA',

    'UNITED ARAB EMIRATES',

    'YEMEN',

    'ARMENIA',

    'AZERBAIJAN',

    'CYPRUS',

    'DJIBOUTI',

    'MALTA',

    'MAURITANIA',

    'SAHRAWI ARAB DEMOCRATIC REPUBLIC',

    'SOMALIA',

    'SUDAN',

    'TURKEY',

);


/* image with a hog */

$img = 'mmm-pork.jpg';

if(in_array($geo_info->country, $mena)){


    /* image with no hog */

    $img = 'cant-have-pork.jpg';

} ?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文