• I′m trying to create different sections of a site based on the country that the users are coming from within the same site. Im trying to create a structure that looks like: mysite.com/dk/content/company1, mysite.com/en/content/company1.. With this I only want it visible for users entering my site from Denmark. I also want the same but for Sweden, Norway, England etc. What is the best/easiest way to create this site structure? I will be using WPML to change the languages shown but I want certain items to be hidden/shown based on where the users access the site from. First page they land on is their country code for example dk is mysite.com/dk as “Home”

    • This topic was modified 8 years, 4 months ago by wille85.
Viewing 1 replies (of 1 total)
  • Well first you have to code to detect user location this following code will help you i am using API

    <?php
    function get_client_ip() {
        $ipaddress = '';
        if (isset($_SERVER['HTTP_CLIENT_IP']))
            $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
        else if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
            $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
        else if(isset($_SERVER['HTTP_X_FORWARDED']))
            $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
        else if(isset($_SERVER['HTTP_FORWARDED_FOR']))
            $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
        else if(isset($_SERVER['HTTP_FORWARDED']))
            $ipaddress = $_SERVER['HTTP_FORWARDED'];
        else if(isset($_SERVER['REMOTE_ADDR']))
            $ipaddress = $_SERVER['REMOTE_ADDR'];
        else
            $ipaddress = 'UNKNOWN';
        return $ipaddress;
    }
    
    $return = array();
    $ip = get_client_ip();
    if (strpos($ip, 'UNKNOWN') !== TRUE)
    {
    $return = json_decode(file_get_contents('https://geoip.nekudo.com/api/'.$ip));
    $code = $return->country->code;
    if (strpos($code, 'US') !== FALSE)
    {   //redirect user to US Page    }
    ?>

    you can use this code in header.php this example is for US visitors for Denmark you have to just replace US country code with Denmark

Viewing 1 replies (of 1 total)
  • The topic ‘Creating different pages for different countries based on Geolocalization’ is closed to new replies.