here is a solution for you @ OP.
Assumption: you are using Wordpress.
add this code to the top of your header.php
Code:
session_start(); // <- We need this to use PHP session storage for a visitor
if (!isset($_SESSION["vistor_geodata"]))
{
$_SESSION["vistor_geodata"] = geoLookupbyIP(get_client_ip_env());
}
go to ipstack.com and sign up for an API key (referred to below as [YOURACCESSKEY].. swap it into the snippet below then
add the code to your theme (or child theme) functions.php file
Code:
//=================================================================================
function geoLookupbyIP($ip)
{
$url="http://api.ipstack.com/".$ip."?access_key=[YOURACCESSKEY]&format=1";
$sRes = file_get_contents($url);
$aRes = json_decode($sRes, true);
return $aRes;
}
function get_client_ip_env() {
$ipaddress = '';
if (getenv('HTTP_CLIENT_IP'))
$ipaddress = getenv('HTTP_CLIENT_IP');
else if(getenv('HTTP_X_FORWARDED_FOR'))
$ipaddress = getenv('HTTP_X_FORWARDED_FOR');
else if(getenv('HTTP_X_FORWARDED'))
$ipaddress = getenv('HTTP_X_FORWARDED');
else if(getenv('HTTP_FORWARDED_FOR'))
$ipaddress = getenv('HTTP_FORWARDED_FOR');
else if(getenv('HTTP_FORWARDED'))
$ipaddress = getenv('HTTP_FORWARDED');
else if(getenv('REMOTE_ADDR'))
$ipaddress = getenv('REMOTE_ADDR');
else
$ipaddress = 'UNKNOWN';
return $ipaddress;
}
Create a new page called "visit casino" which resides at yoursite dot com/visit-casino
take a note of the page ID... (when editing it) we will refer to this is REDIRECTPAGEID and insert it into the following code block which will also go at the top of your functions.php
Code:
$ipartnerRedirectPageID= REDIRECTPAGEID ;
add_filter('init','flushRules');
add_filter('query_vars', 'add_my_var');
function add_my_var($public_query_vars)
{
$public_query_vars[]= 'casino_name';
return $public_query_vars;
}
function flushRules(){
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
we are setting things up here so that our "visit-casino" page accepts an extra path which is used as a parameter (casino_name)
so it will be like yoursite dot com/visit-casino/888-Casino or something like that. We will know the users GEO, so we can use a lookup table to see if we have a suitable affiliate link / landing page for that specific geo. (if not we will serve our default link). We should do a lookup to see if country is in allowed countries list for merchant, but that is outside scope of this post.
anyway.. so let's set up a master lookup sheet in CSV format so that we can access it with code and store the data in a structured manner..
here's how I manage the affiliate links on 1 of my sites:

create the sheet like this, publish to the web (publish=>public on the web, this sheet only, CSV) and take a note of the CSV address.
swap it into the bit below that says 'YOUR_GOOGLE_DOCS_OR_LOCALCSV_FILE_LINK'.
Now we are almost done.. Let's create a function to accept a merchant name (to lookup, and a geo code), and which will return the appropriate link for the visitor.
put this in functions.php
Code:
function getAffiliateLink($sCasinoPartner, $sCountryCode)
{
$sCountryCode=strtolower($sCountryCode);
$sLink =null;
$sURL = "YOUR_GOOGLE_DOCS_OR_LOCALCSV_FILE_LINK";
$aData = array_map('str_getcsv', file($sURL));
$aDataNew=[];
$aHeadings = [];
foreach ($aData as $index=>$aRow)
{
if ($index==0) //header row
{
foreach ($aRow as $i=>$v)
$aHeadings[]=$v;
}
else
{
$sMerchant= $aRow[0];
//echo $teamindex;
foreach ($aRow as $i=>$v)
{
//echo $i.'<br/>';
if ($i!=0)
{
$aDataNew[$sMerchant][$aHeadings[$i]]=$v;
}
}
}
}
foreach ($aDataNew as $sCasino=>$aAffiliateLinks)
{
if (strtolower($sCasinoPartner) === strtolower($sCasino) )
{
if (isset($aAffiliateLinks[$sCountryCode]))
{
if ($aAffiliateLinks[$sCountryCode]!="")
$sLink = $aAffiliateLinks[$sCountryCode];
else
$sLink= $aAffiliateLinks["default_link"];
}
else //serve default link for merchant
$sLink= $aAffiliateLinks["default_link"];
}
}
return $sLink;
}
Now you just need to send the visitor to the right page and the page template assigned to the visit-casino page should do the rest...
take a note of your template path relative to your theme (or child theme root).
mine is "templates/template-partner-redirect.php".. (you will need to refer to this in a later step)
so modify your visit-casino page template (ie yourthemefolder/templates/template-partner-redirect.php) and add the following code under the call to get_header();
Code:
global $post, $aMeta;
$sLink=null;
$aQueryVars = $wp_query->query_vars;
$sSiteName = str_replace("-"," ",$aQueryVars["casino_name"]);
if (isset($aQueryVars["casino_name"]))
$sLink = getAffiliateLink(str_replace("-"," ",$aQueryVars["casino_name"]), $_SESSION["vistor_geodata"]["country_code"]);
now we have the correct link in a variable $sLink.. we can echo it to the screen in the template for the user to click like so:
Code:
<h1>Transferring you to <?php echo $sSiteName;?></h1>
<p>If your browser does not redirect you, <a href="<?php echo $sLink;?>">please click here</a>.</p>
and to automagically redierect the user after 3 seconds...
add this to header.php in the <HEAD> section, right after your analytics, and before the call to wordpress wp_head();
Code:
if (is_page_template('templates/template-partner-redirect.php'))
{
$aQueryVars = $wp_query->query_vars;
$sSiteName = str_replace("-"," ",$aQueryVars["casino_name"]);
$sLink="";
if (isset($aQueryVars["casino_name"]))
$sLink = getAffiliateLink(str_replace("-"," ",$aQueryVars["casino_name"]), $_SESSION["vistor_geodata"]["country_code"]);
if ($sLink!="")
{
?>
<script>
setTimeout(function(){
window.location.href = "<?php echo $sLink;?>";
}, 5000);
</script>
<?php
}
}
?>
cliffnotes:
geo detect user.
store their geo country code in the "php session"
when they visit the visit-casino page, a bit of code does a lookup to a csv (which we could host on google sheets + publish)
so now when anyone who visits /visit-casino, their countrycode and the casino name (now a "parameter") is used to retrieve the correct affiliate link, and redirect the user to the appropriate country landing page, serving the default link if none is found.
If everything goes swimmingly and I haven't left anything out, you are now rocking and rolling with dynamic geo-served affiliate links.
oooooooh yea.