Jump to content

Custom HTML Panel for specific country


faisal

Recommended Posts

Hi all

 

I have a simple Custom HTML Panel

<?php

if (!defined("WHMCS"))
   die("This file cannot be accessed directly");

use WHMCS\View\Menu\Item as MenuItem;


add_hook('ClientAreaSecondarySidebar', 1, function (MenuItem $secondarySidebar) {

   $customMessage = $secondarySidebar->addChild('Custom Title');
   $customMessage->moveToBack();

   $customMessageHtml = <<<EOT
       <div>My custom HTML message.</div>
EOT;

   $secondSidebar = $secondarySidebar->getLastChild();
   $secondSidebar->setBodyHtml($customMessageHtml);

});

 

 

Can I display the panel only for visitor from specific country by country iso code? for example "fr" or "de"?

Link to comment
Share on other sites

Can I display the panel only for visitor from specific country by country iso code? for example "fr" or "de"?

if the visitor was a client and logged in, then yes you could easily do it in the hook; if this is a sidebar for everyone, then you'd have to use a geo-ip service to convert the IP address to a country.

Link to comment
Share on other sites

try the following - change the 'FR' value to the ISO code value for the country that you want to allow to see the panel... e.g it currently shows the panel to users in France, any other country won't see it.

 

<?php

if (!defined("WHMCS"))
   die("This file cannot be accessed directly");

use WHMCS\View\Menu\Item as MenuItem;

add_hook('ClientAreaSecondarySidebar', 1, function (MenuItem $secondarySidebar) {

   $current_country = '';
   $ret = file_get_contents("http://api.hostip.info/get_json.php?ip=".$_SERVER['REMOTE_ADDR']);
   $parsed_json = @json_decode($ret,true);
   if(isset($parsed_json['country_code'])) {
       $current_country = $parsed_json['country_code'];
   }

   if ($current_country!="FR"){
       return;
   }

   $customMessage = $secondarySidebar->addChild('Custom Title');
   $customMessage->moveToBack();

   $customMessageHtml = <<<EOT
       <div>My custom HTML message.</div>
EOT;

   $secondSidebar = $secondarySidebar->getLastChild();
   $secondSidebar->setBodyHtml($customMessageHtml);

});

the country code part is taken from the Geo-IP hook @ http://www.blog.modulesgarden.com/automatic-currency-and-language-setup-for-whmcs/ - if it is too slow for your needs (as it currently queries another website to determine the country), upload the Maxmind database to your server and use the code (parts 4, 5 & 6) for maxmind in the blog.

Link to comment
Share on other sites

First of all: Thank you for your time and help.

 

 

Second:

 

The hostip.info didn't work for me at all and with simple test I see that not all countries are listed (at least my country or my IP, I am really not sure):

<?php

if (!defined("WHMCS"))
   die("This file cannot be accessed directly");

use WHMCS\View\Menu\Item as MenuItem;

add_hook('ClientAreaSecondarySidebar', 1, function (MenuItem $secondarySidebar) {


   $current_country = '';

   $ret = file_get_contents("http://api.hostip.info/get_json.php?ip=".$_SERVER['REMOTE_ADDR']);
   $parsed_json = @json_decode($ret,true);
   if(isset($parsed_json['country_code'])) {
       $current_country = $parsed_json['country_code'];
   }

   if ($current_country != "FR"){
       //return;
   }

   $customMessage = $secondarySidebar->addChild('Custom Title');
   $customMessage->moveToBack();

   $customMessageHtml = <<<EOT
       <div>My custom HTML message.</div>
       <div>Country Code: $current_country</div>
EOT;

   $secondSidebar = $secondarySidebar->getLastChild();
   $secondSidebar->setBodyHtml($customMessageHtml);

});

It will show:

My custom HTML message.

Country Code: XX

 

 

 

So I did it with maxmind and it's working

<?php
if (!defined("WHMCS"))
   die("This file cannot be accessed directly");

use WHMCS\View\Menu\Item as MenuItem;

add_hook('ClientAreaSecondarySidebar', 1, function (MenuItem $secondarySidebar) {


   $current_country = '';

   /*
   $ret = file_get_contents("http://api.hostip.info/get_json.php?ip=".$_SERVER['REMOTE_ADDR']);
   $parsed_json = @json_decode($ret,true);
   if(isset($parsed_json['country_code'])) {
       $current_country = $parsed_json['country_code'];
   }
   */

   include "geoip.inc";
   $gi = geoip_open(dirname(__FILE__).DIRECTORY_SEPARATOR."GeoIP.dat", GEOIP_STANDARD);
   $current_country = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
   geoip_close($gi);

   if ($current_country != "FR"){
       //return;
   }

   $customMessage = $secondarySidebar->addChild('Custom Title');
   $customMessage->moveToBack();

   $customMessageHtml = <<<EOT
       <div>My custom HTML message.</div>
       <div>Country Code: $current_country</div>
EOT;

   $secondSidebar = $secondarySidebar->getLastChild();
   $secondSidebar->setBodyHtml($customMessageHtml);

}); 

Now it is showing the Country Code:

My custom HTML message.

Country Code: (It's Showing the country code)

 

 

* I just comment/disable the return in the if condition for testing only so I can see the Country Code in all cases

Link to comment
Share on other sites

If I use my browser go to http://api.hostip.info/get_json.php?ip=My-IP-address

 

All I get is:

 

{"country_name":"(Unknown Country?)","country_code":"XX","city":"(Unknown City?)","ip":"My-IP-address"}

 

 

 

Maybe something wrong from my end because even here in the forum can't edit my posts! and if i wrote something and hit the review button I always get a human verification page, and sometimes when I do it my post deleted and had to write again.

Link to comment
Share on other sites

First of all: Thank you for your time and help.

my pleasure. :idea:

 

The hostip.info didn't work for me at all and with simple test I see that not all countries are listed (at least my country or my IP, I am really not sure):

the maxmind database option should be more accurate anyway, so I wouldn't worry about hostip if you don't need to use it - it worked for me in the UK.

 

Maybe something wrong from my end because even here in the forum can't edit my posts! and if i wrote something and hit the review button I always get a human verification page, and sometimes when I do it my post deleted and had to write again.

what happens when you go to http://www.hostip.info/use.html - does it show your country flag? if not, it probably just means your IP address/block is not in their database...

Link to comment
Share on other sites

what happens when you go to http://www.hostip.info/use.html - does it show your country flag? if not, it probably just means your IP address/block is not in their database...

 

No flag. just a question mark image with a funny message that says: ... actually we haven't a clue. :roll: lol

 

 

I will use maxmind :idea:

 

Thanks

 

- - - Updated - - -

 

They let my add my Ip :)

 

 

Is this wrong? Make a correction (link) that go to http://www.hostip.info/correct.html?spip=your-IP

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated