PHP code defaulting to an error when it shouldn't be - php

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;
}
}
}
?>

Maybe try loading all the valid values, and checking to make sure it's valid, even if it's not NJ/DE.
if (in_array($state, array('NJ', 'DE'))) {
// Redirect
} elseif (!in_array($states, $all_states_array)) {
// Go to error.
}
The else is implied, in that the script will just continue working. You could structure this several different ways, depending on how much you need to extend it:
if (!in_array($state, $all_states_array)) {
// Error
}
if (in_array($state, array('NJ', 'DE'))) {
// Redirect
}
You could also add all the cases:
case 'DE':
// Do something;
break;
case 'NJ':
// Do soemthing;
break;
case 'PA':
case 'AL':
case 'NY':
case 'OK':
case 'TX':
...
// Valid, but not the right target.
break;
default:
// show error

Try this. If the state is not from NJ or DE, then do nothing.
<?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';
header('Location: '.$url);
die();
break;
case DE:
$url = 'http://example.com';
header('Location: '.$url);
die();
break;
default:
}
$reader->close();
} 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;
}
}
}
?>

Related

PHP Include - Error Fixing

I have the following PHP code that produces an error as the include files don't exist. I have not yet made them but I want to stop errors being produced (not just hidden). Is there anything I can put into my code that says "don't record any errors if the file doesn't exist, just ignore the instruction"
<?php
$PAGE = '';
if(isset($_GET['page'])) {
$PAGE = $_GET['page'];
};
switch ($PAGE) {
case 'topic': include 'topic.php';
break;
case 'login': include 'login.php';
break;
default: include 'forum.php';
break;
};
?>
use file_exists() to check if your file exists or not before calling include;
if (file_exists('forum.php')) {
//echo "The file forum.php exists";
include 'forum.php';
}
//else
//{
// echo "The file forum.php does not exists";
//}
Include the files only if they exist. You can add a check for existing file -
switch ($PAGE) {
case 'topic':
if(file_exists(path_to_file)) {
include 'topic.php';
}
break;
......
};
file_exists()
You seem to be looking for # operator which silences any errors from an expression, you can read more about it here: http://php.net/manual/en/language.operators.errorcontrol.php
Use file_exists() function:
<?php
$PAGE = '';
if(isset($_GET['page'])) {
$PAGE = $_GET['page'];
};
switch ($PAGE) {
case 'topic':
if (file_exists("topic.php")){
include 'topic.php';
}
break;
case 'login':
if (file_exists("login.php")){
include 'login.php';
}
break;
default:
if (file_exists("forum.php")){
include 'forum.php';
}
break;
};
?>
Documentation http://php.net/manual/en/function.file-exists.php

Rotate Links Using Geoplugin

I'm geoplugin.class to redirect CA users to a specific link.
Right now the code only allows me to redirect the user to 1 website. I would like to modify this code so I can redirect the user to either
link1.com
link2.com
link3.com
Does anyone have a quick modification for this?
Thank you in advance.
<?php
require_once('geoplugin.class.php');
$geoplugin = new geoPlugin();
$geoplugin->locate();
$geo_region = $geoplugin->region;
switch($geo_region) {
case 'CA':
header('Location: http://www.link1.com');
exit;
default: // exceptions
header('Location: http://www.everyoneelsegoeshere.com');
exit;
}
?>
Try This one:
require_once('geoplugin.class.php');
$geoplugin = new geoPlugin();
$geoplugin->locate();
$geo_region = $geoplugin->region;
switch($geo_region) {
case 'CA':
$links = array('link1.com','link2.com','link3.com');
header('Location: http://'.$links[array_rand($links)]);
exit;
default: // exceptions
header('Location: http://www.everyoneelsegoeshere.com');
exit;
}

Determining user language based on IP address (geolocation)

I've integrated two pieces of PHP code found on different websites. The first part of code finds out what country you are in and the second one determines the language. I have tried running it but with no luck. It does not give me a error, just a blank white screen.
Here is the code:
<?php
function visitor_country()
{
$client = #$_SERVER['HTTP_CLIENT_IP'];
$forward = #$_SERVER['HTTP_X_FORWARDED_FOR'];
$remote = $_SERVER['REMOTE_ADDR'];
$result = "Unknown";
if(filter_var($client, FILTER_VALIDATE_IP))
{
$ip = $client;
}
elseif(filter_var($forward, FILTER_VALIDATE_IP))
{
$ip = $forward;
}
else
{
$ip = $remote;
}
$ip_data = #json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=".$ip));
if($ip_data && $ip_data->geoplugin_countryName != null)
{
$result = $ip_data->geoplugin_countryName;
}
return $result;
}
session_start();
header('Cache-control: private'); // IE 6 FIX
if (isset($_GET['lang'])) {
$lang = $_GET['lang'];
// register the session and set the cookie
$_SESSION['lang'] = $lang;
setcookie('lang', $lang, time() + (3600 * 24 * 30));
} else if (isset($_SESSION['lang'])) {
$lang = $_SESSION['lang'];
} else if (isset($_COOKIE['lang'])) {
$lang = $_COOKIE['lang'];
} else {
$lang = 'en';
}
switch ($lang) {
case 'en':
$lang_file = 'lang.en.php';
break;
case 'de':
$lang_file = 'lang.de.php';
break;
if(visitor_country() == "Germany") {
default:
$lang_file = 'lang.de.php';
echo "Germany";
} else {
default:
$lang_file = 'lang.en.php';
echo "Not in Germany";
}
}
include_once 'languages/' . $lang_file;
?>
I could not include the language code, but rest assured it works as I use it on my site without auto-country detection.
You code returns an error: PHP Parse error: syntax error, unexpected 'default' (T_DEFAULT) in yourfile.php on line. It means that you're misusing default in switch statement. Replace your last part of your code with this:
default:
if(visitor_country() == "Germany") {
$lang_file = 'lang.de.php';
echo "Germany";
} else {
$lang_file = 'lang.en.php';
echo "Not in Germany";
}
Take out default from inside of if/else statement.
EDIT 1:
Make sure that PHP displays ERRORS, WARNING and NOTICES to properly debug your code:
ini_set('display_errors', -1);
EDIT 2:
If it was working without the switch statement, like you said, then you must make sure that files lang.en.php/lang.de.php really exist.
EDIT 3:
You are suppressing errors with having # in-front of json_decode(file_get_contents. Most likely you would have to edit your php.ini and enable allow_url_fopen to make it work. I bet if you remove #, you will get an error:
Warning: file_get_contents(): http:// wrapper is disabled in the server configuration by allow_url_fopen=0 in
I wouldn't strongly recommend using this kind of method to detect user language and enabling allow_url_fopen as it's possible security flaw! In case you're interested, I will provide you with much better solution for determining user browsers' language.

Geographically change domain keeping URL same

i am using this script to redirect users according to their IP.
But the problem is it redirects to homepage or only to URL i specify in the script. how can i just change the domain keeping the URL same? for example site.com/whateverpage redirected to site.au/whateverpage.
<?php
// Next two lines are for Beyond Hosting
// Don't forget to change your-domain
require_once '/home/your-domain/php/Net/GeoIP.php';
$geoip = Net_GeoIP::getInstance('/home/your-domain/php/Net/GeoIP.dat');
// Next two lines are for HostGator
require_once 'Net/GeoIP.php';
$geoip = Net_GeoIP::getInstance('GeoIP.dat');
try {
$country = $geoip->lookupCountryCode($_SERVER['REMOTE_ADDR']);
switch((string)$country) {
case 'AU':
$url = "http://www.site.au";
break;
case 'CA':
$url = "http://www.site.ca";
break;
default:
$url = "http://site.com";
}
if (strpos("http://$_SERVER[HTTP_HOST]", $url) === false)
{
header('Location: '.$url);
}
} catch (Exception $e) {
// Handle exception
}
?>
Add $_SERVER['REQUEST_URI'] to your redirection.
header("Location: $url/$_SERVER[REQUEST_URI]");

Redirect Loops in Switch Case with header(location)

<?php
$rexgeoip = new RexGeoIp;
$iso = strtoupper( $rexgeoip->getCountryIso() );
switch ($iso) {
case 'DE':
header("Location: http://www.domain.de/en/",TRUE,301);
break;
case 'AT':
header("Location: http://www.domain.de",TRUE,301);
break;
case 'CH':
header("Location: http://www.domain.de",TRUE,301);
break;
default:
header("Location: http://www.domain.de/en/",TRUE,301);
break;
}
}
echo "<!-- your iso is $iso -->";
?>
This is my code which redirects to the corresponding domain path.
I changed the DE case to /en because I'm in Germany and want to test the redirect.
But every time I hit with DE ISO I get a "to many redirects" timeout. This also happens if I connect via a web-proxy from US or Asia.
Any ideas or suggestions?
Add de to the path and don't redirect in the script which is responsible for http://www.domain.de/de/ or for http://www.domain.de/en/:
<?php
$rexgeoip = new RexGeoIp;
$iso = strtoupper( $rexgeoip->getCountryIso() );
switch ($iso) {
case 'DE':
header("Location: http://www.domain.de/en/",TRUE,301);
exit;
break;
case 'AT':
header("Location: http://www.domain.de/de/",TRUE,301);
exit;
break;
case 'CH':
header("Location: http://www.domain.de/de/",TRUE,301);
exit;
break;
default:
header("Location: http://www.domain.de/en/",TRUE,301);
exit;
break;
}
}
echo "Script never gets here";
?>

Categories