0

I'm trying to show a div that will depend on the country and I'm a bit lost on how to do it. My site is running on Wordpress. So what should happen is when, for example, the country is Singapore, Cambodia and Vietnam, this specific section appears (like 'This product is available in Singapore') and if it's not the three, a different section will appear (like 'This product is not available in the Philippines')

How do I achieve this?

user3079066
  • 43
  • 1
  • 6

1 Answers1

1

To figure out user's country, one may use Sypex Geo: http://sypexgeo.net/ru/download/ It's BSD-licensed. Unfortunately it has only Russian docs, but this is how to use it:

  1. Copy SxGeo.php script file and SxGeo.dat country database file to your hosting

  2. Include SxGeo.php in your Wordpress theme functions.php, or create a widget which will show product availability info and include it there.

    include('SxGeo.php');

  3. Create SxGeo object

    $SxGeo = new SxGeo(); // default mode // or $SxGeo = new SxGeo('SxGeo.dat', SXGEO_BATCH | SXGEO_MEMORY); // fastest mode, uses more RAM

  4. Detect the country. It is considered here that you have stored user's IP in $ip. I am not able to give you a perfect advice on how to detect user's IP, because a user can be behind a proxy server. You may want to look at this topic: How to get the client IP address in PHP? But, I believe, in many cases proxy server is in the same country, as its user.

    $ip = $_SERVER['REMOTE_ADDR']; $country = $SxGeo->getCountry($ip); // return two-letters country ISO-code

  5. Remove SxGeo object, freeing up resources

    unset($SxGeo);

Community
  • 1
  • 1
Mikhail Batcer
  • 1,938
  • 7
  • 37
  • 57