I had a PHP developer create a redirection script that redirects users in specific states to another URL, while letting everyone else visit the website.
The problem is it's redirecting everyone who doesn't match a state listed to the error URL, when it should be letting them visit the site.
I think there's a return missing? What do you guys think?
<?php
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
require_once '/vendor/autoload.php';
use MaxMind\Db\Reader;
$databaseFile = '/geoip/GeoIP2-City.mmdb';
$ipWhiteList = ['123', '321'];
if(!in_array($_SERVER["REMOTE_ADDR"], $ipWhiteList)) {
$reader = new Reader($databaseFile);
$iso_code = $reader->get($_SERVER["REMOTE_ADDR"])['subdivisions'][0]['iso_code'];
if (!isset($_REQUEST['HTTP_REFERER'])) {
switch($iso_code) {
case NJ:
$url = 'http://example.com';
break;
case DE:
$url = 'http://example.com';
break;
default:
$url = 'http://www.example.com/?=error';
break;
}
$reader->close();
header('Location: '.$url);
die();
} else {
if(strpos($_SERVER['HTTP_REFERER'], "example2.com") > -1) {
echo "You were redirected from ".urldecode($_REQUEST['referer']).", but it is not available in your area (".$iso_code.").";
break;
} else {
echo "Welcome!";
break;
}
}
}
?>
Try this. If the state is not from NJ or DE, then do nothing.