2015-06-29 23 views
0

我試圖根據用戶位置顯示內容。例如,顯示是否在澳大利亞以外的國家。根據位置使用PHP顯示內容

我一直在使用此鏈接中的文件: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"; 
} 
?> 

乾杯

+0

你如何改變位置進行測試呢? – developerwjk

+0

你已經在迴應上面的國家名稱,它與「澳大利亞」不同嗎?另外@developerwjk代理或只是僞造了geoplugin結果。 –

+0

將位置更改爲澳大利亞境外時,「$ geoplugin-> countryName」的結果是什麼? – Drakes

回答

0

就個人而言,如果我是你,我會通過countryCode而不是countryName去。從某種意義上說,它更「可控」。

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

在另一方面,什麼是您的countryName/countryCode說明了什麼?任何一個變量都存在什麼?

確保已彙報錯誤,包住您的通話失敗:

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