I have a client who needs to have a site serve slightly different content based on region (US/UK are the only two, thankfully).
Now, I looked into the ip to nation module, but thanks to some wonderful code oversight it uses (ip_address} as its only tag - which when used within the exp:weblog:entries tag shows the IP of the author NOT the user - and won’t accept the alternate syntax.
I’ve installed fresh variables, and am using the following code to return the user’s country (swiped from the interwebs, but it works okay).
<?php
function countryCityFromIP($ipAddr)
{
//function to find country and city name from IP address
//verify the IP address for the
ip2long($ipAddr)== -1 || ip2long($ipAddr) === false ? trigger_error("Invalid IP", E_USER_ERROR) : "";
// This notice MUST stay intact for legal use
$ipDetail=array(); //initialize a blank array
//get the XML result from hostip.info
$xml = file_get_contents("http://api.hostip.info/?ip=".$ipAddr);
//get the city name inside the node <gml:name> and </gml:name>
preg_match("@<Hostip>(\s)*<gml:name>(.*?)</gml:name>@si",$xml,$match);
//assing the city name to the array
$ipDetail['city']=$match[2];
//get the country name inside the node <countryName> and </countryName>
preg_match("@<countryName>(.*?)</countryName>@si",$xml,$matches);
//assign the country name to the $ipDetail array
$ipDetail['country']=$matches[1];
//get the country name inside the node <countryName> and </countryName>
preg_match("@<countryAbbrev>(.*?)</countryAbbrev>@si",$xml,$cc_match);
$ipDetail['country_code']=$cc_match[1]; //assing the country code to array
//return the array containing city, country and country code
return $ipDetail;
}
$IPDetail=countryCityFromIP($_SERVER['REMOTE_ADDR']);
echo $IPDetail['country_code']; //country of that IP address
?>Which when used in fresh variables returns the short code for the user’s country nicely.
However, using it in an {if} statement is apparently a dead end.
All I want is for it to say
{if user_country == "USA"}
USA content
{if:else}
Rest of world
{/if}Where (user_country} is the fresh variable containing the php, but currently no dice.
Running 1.6.9 - it’s a fresh install so everything’s the most recent build.
Any help would be appreciated! I’ve spent the whole day looking into solutions and if this can work it’ll be perfect….