0

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.

Community
  • 1
  • 1
Nitin
  • 23
  • 1
  • 6
  • Not sure what is wrong here. Alternate approach: use a module like [ip2country](https://drupal.org/project/ip2country) – msound Apr 24 '14 at 06:44
  • Actually I want to show flag and contact no. on my homepage of website according to country, so user can contact there. – Nitin Apr 24 '14 at 09:27
  • There is [ip2cc](https://drupal.org/project/ip2cc) module, but it is only for Drupal 6. But you can see the Additional Information section on that page for related modules. – msound Apr 24 '14 at 14:08

1 Answers1

0

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