Is it possible to have a static IP address included in this?

Is there any way of adjusting the code below, to also allow a static IP that isn’t within the allowed countries please?


$config['redirect_to']='http://www.websiteaddress.com/';		//redirection url for visitors from not allowed countries
$config['allowed_countries']='at,be,ch,de,dk,es,eu,fi,fr,gb,gg,gi,gr,ie,im,it,je,my,nl,no,pt,se,tr,tz,us';				//allowed countries codes

Any help much appreciated.

Dez

There are several options, but it really depends on how you’re using the $config[‘allowed_countries’] variable. Are you just exploding it and then comparing it to something grabbed from $_SERVER, for instance? You could always just add the IP to the end of that list and use an extra condition in your check:


// assumed $visitorCountry and $allowedStaticIps are set already
$allowed = explode(',', $config['allowed_countries']);

if (!in_array($visitorCountry, $allowed) && !in_array($allowedStaticIps, $allowed)) {
  header("Location: $config['redirect_to']");
}

But that could get messy depending on how you extend this further. Personally I would just have the static IPs saved in a separate variable and do a check there too.

Many thanks LeonardChallis. It might only get a max of 3-4 IP’s to add. If it were you, how would you do it please?

I’d do something like this:


/* presuming this is how the $config variable would end up - not necessarily how you'd create it */
$config = array(
  'allowed_countries' => array('at', 'be', 'ch', 'de', 'dk', 'es', 'etc'),
  'allowed_ips' => array('192.168.0.1', '12.34.56.78', 'etc'),
  'redirect' => 'http://www.byebye.com/'
);

/* grab their details */
$user = array(
  'ip' => $_SERVER['REMOTE_ADDR'],
  'country' => /* not sure what you're doing here, maybe something like... */ substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2)
);

if (!in_array($user['country'], $config['allowed_countries']) && !in_array($user['ip'], $config['allowed_ips'])) {
  header('Location: ' . $config['redirect']);
}

echo "Welcome back my globally superior friends! ;)";

This is a tad more long winded than it has to be, but will be better for readability and extensibility in my opinion.

Good luck!