根据位置重定向页面

Please help me, and be patient since i'm a php virgin.

I'm using Cloudflare and want to redirect every page from outside of Indonesia to a specific page. After a few tries, I can redirect from index.php to each separate destination page.

But the problem comes when I put this redirect code on the destination page. It seems like it loops indefinitely. I want to keep this functionality on, because I don't want a visitor that clicks on a Google search result to end up in the wrong page.

How can I properly check the IP on every page, and if they are from ID, then they will be redirected to index_id.php. And if they are not, they will go to index_ww.php. This without infinite loops, because this code is in both index_id.php and index_ww.php.

$country_code = $_SERVER["HTTP_CF_IPCOUNTRY"];

if ($country_code=="ID"){
    $link = 'http://www.myweb.com/index_id.php';
} else {
    $link = 'http://www.myweb.com/index_ww.php';
}
header("location:$link");
exit;

Try adding a query parameter onto the end of the destination URL, like ?redirected=1, and always check for that parameter before performing the redirect code. Here's an example:

<?php

if (empty($_GET['redirected'])) {
    $country_code = $_SERVER["HTTP_CF_IPCOUNTRY"];

    if ($country_code=="ID"){
        $link = 'http://www.myweb.com/index_id.php?redirected=1';
    } 
    else {
        $link = 'http://www.myweb.com/index_ww.php?redirected=1';
    }

    header("location:$link");
    exit;
}

?>