I have php code for geoplugin, it's working fine, if I run it as a normal php file but when I add it in drupal site in .tpl.php file, it's not working at all. I have tried many ways which I got from here! and other also but not comming at all. and I am using var_dump(..) which is contains geoplugin array but values are null only. recently my code is which I am using:
function visitor_country()
{
$client = @$_SERVER['HTTP_CLIENT_IP'];
$forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
$remote = $_SERVER['REMOTE_ADDR'];
$result = "Unknown";
if(filter_var($client, FILTER_VALIDATE_IP))
{
$ip = $client;
}
elseif(filter_var($forward, FILTER_VALIDATE_IP))
{
$ip = $forward;
}
else
{
$ip = $remote;
}
$ip_data = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=".$ip));
if($ip_data && $ip_data->geoplugin_countryName != null)
{
$result = $ip_data->geoplugin_countryName;
}
return $result;
}
Please tell me what's wrong with me here.
The general idea is that you shouldn't write any buisness logic in tpl.php files at all. The php code will work, but it is not right. You should have a separate module for that logic. Have you tried to find the appropriate module on drupal.org? I wrote the integration for geoplugin.com and Drupal on php, you can check it here: https://www.drupal.org/node/2452375. By the time you read this it may be included in the stable release of the module, you can check it on the project's page here https://www.drupal.org/project/geoplugin