使用PHP显示基于位置的内容

时间:2015-06-29 23:46:22

标签: php geolocation location

我正在尝试根据用户位置显示内容。例如,如果在国外,澳大利亚显示。

我一直在使用此链接中的文档:http://www.geoplugin.com/webservices/php

这是我一直在使用的代码。即使我改变位置,它也会始终显示回声。

<?php
require_once('geoplugin.class.php');
$geoplugin = new geoPlugin();
// If we wanted to change the base currency, we would uncomment the following         line
// $geoplugin->currency = 'EUR';

$geoplugin->locate();

echo "Geolocation results for {$geoplugin->ip}: <br />\n".

"Country Name: {$geoplugin->countryName} <br />\n".
"Country Code: {$geoplugin->countryCode} <br />\n";

if ( $geoplugin->countryName != "Australia" ) {
//our visitor is not using the same currency as the base currency
echo "Show International Popup";
}
?>

干杯

1 个答案:

答案 0 :(得分:0)

就个人而言,如果我是你,我会通过countryCode代替countryName。它更容易包含&#34;从某种意义上说。

if($geoplugin->countryCode !== "AU") {
    // load your selector
}

另一方面,countryName / countryCode显示了什么?两个变量中是否存在任何内容?

确保您有错误报告,请阻止您的通话失败:

ini_set('display_errors', 1);
display_errors(-1); // or use E_ALL