3

On a Drupal 6 site I am trying to display blocks on my site based on user's country.

  1. To do this I had to install 'IP to Country' module
  2. Updated IP to Country
  3. Created a new block and added the following code into the block body to print the user's country code:

    <?php
    $co=module_invoke('ip2cc','get_country',$_SERVER['REMOTE_ADDR'];);
    print($co->country_code);
    ?>
    
  4. Then, under 'Show block on specific pages' selected 'Show if the following PHP code returns TRUE (PHP-mode, experts only).' and then added the below mentioned code, so that the block is displayed based on the country ID.

    <?php $co->country_code == "IN" ?>
    

After doing all these the block is getting disabled for the ID mentioned, eg: If 'IN' is mentioned the block should only be displayed for Indian visitors, but the block is actually being disabled.

  • did u load ip database? – monymirza Mar 29 '13 at 6:17
  • Yes I did load the IP database, and confirmed by downloading the 'ip-to-country.csv' file. – AnAnD Mar 29 '13 at 6:20
1

Your code seems to be right. You've mis-spelled the module name in the module_invoke function at #3 and misplaced the semicolon ;

Try this:

<?php

$detectedcountry = module_invoke('ip2country','get_country',$_SERVER['REMOTE_ADDR']);
$us_canada = array(
  'US','CA'
);

//return true in block visibility when the country appears to be in detected country
if(in_array($detectedcountry,$us_canada)){
  return true;
} 

?>
  • I tried above mentioned code, but it is not working for me for 'IN' country code. – AnAnD Mar 29 '13 at 6:28
  • @AnAnD try to print the value of the $detectedcountry array in your custom module, using some hook (hook_init would help); and check to see it the countries are actually being loaded. Except that I don't think there might be a problem here. – AjitS Mar 29 '13 at 6:32
  • btw, I am using drupal.org/project/ip2cc module and the module name is ip2cc – AnAnD Mar 29 '13 at 6:36
  • I am able to print the country code using the code that I have mentioned above. – AnAnD Mar 29 '13 at 6:38
  • 1
    No need to say sorry ;-) Just put return TRUE; in your code at #4 after if($co->country_code == 'IN'). – AjitS Mar 29 '13 at 6:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.