Question

I'm using the Maxmid geoip script (php) to redirect users based on their location (www.mysite.com - for the purposes of this question). uk visitors will go to the uk site, whereas others will stay on the current site. however, i also want to set up an exception, just for my ip so that i am able to view both sites (provided my ip matches up with that given in the file - i am based in the UK). below is my coding which doesn't work with regard to the "exception ip".

<?php
require_once($_SERVER['DOCUMENT_ROOT']."/geoip/geoip.inc");
$gi = geoip_open($_SERVER['DOCUMENT_ROOT']."/geoip/GeoIP.dat", GEOIP_MEMORY_CACHE);
$country = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
geoip_close($gi);
$useragent_country = array('gb');
$exception_ip = "80.47.200.21";

if (in_array(strtolower($country), $useragent_country)){
    header('location: http://www.mysite.co.uk');
    exit();
} 
if ($_SERVER['REMOTE_ADDR'] = $exception_ip){
    header('location: http://www.mysite.com');
}
?>

furthermore, to keep the "exception ip" on the current site, is my coding correct?

Was it helpful?

Solution

I did it in the end. putting the exception ip in an array means i can add as many as i want. also, the $uri means it will go to the same path upon redirect:

<?php
require_once($_SERVER['DOCUMENT_ROOT']."/geoip/geoip.inc");
$gi = geoip_open($_SERVER['DOCUMENT_ROOT']."/geoip/GeoIP.dat", GEOIP_MEMORY_CACHE);
$country = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
geoip_close($gi);
$useragent_country = array('gb');
$uri = $_SERVER['REQUEST_URI'];
$exception_ip = array("1.2.3.4.5");//ADD IPs here in array form

if(in_array($_SERVER['REMOTE_ADDR'], $exception_ip)){

//do nothing

}
else{
if (in_array(strtolower($country), $useragent_country)){
    header('location: http://www.mysite.co.uk$uri');//redirect to same path
    exit();
} 
}
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top