Geographically change domain keeping URL same - php

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]");

Related

header Location is going back to the same POST URL it came from

For some reason, my header("Location: <url>") is getting redirected to the same POST url it came from. I had to switch to echo a javascript redirect to make it work for the time-being.
catch (Exception $e)
{
switch (get_class($e))
{
case 'ValidationError':
$_SESSION['FLASH_ERROR'] = $e->getMessage();
$location = CB_ABS_URI."upgrade?ID=".$_GET['ID']."&package=".$_POST['plan'];
# die("You're getting redirected ... <script type='text/javascript'>setTimeout('redirect()',0); function redirect() { window.location = '$location'; }</script>");
header("Location:", $location);
exit;
break;
default:
# die
break;
}
}
I think its a typo.
header("Location:", $location);
Should be
header("Location: ". $location);
You had a comma in the header function.
If you add a comma in the header function your setting the replace parameter in that function as mentioned on php.net

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

PHP Redirect just once with GeoIp2

I am using MaxMind's GeoIp2 PHP to redirect website visitors based on their country.
I have managed to get the redirect working so that:
US visitors go to http://www.example.com/us
Malaysian visitors go to http://www.example.com/my
All other visitors go to http://www.example.com
The problem is that I only want to redirect visitors once.
After they are on the website, if they navigate to http://www.example.com
they should be able to do so without getting redirected, regardless of their country.
This is so that both humans and spiders can still have the freedom to visit pages that are not targeted at their country.
I have tried using the suggestion to a similar problem as answered here
but the question there is regarding different domains for different countries instead of different paths so the solution doesn't work for me.
The code:
<?php
require_once '../vendor/autoload.php';
use GeoIp2\Database\Reader;
$reader = new Reader('/usr/local/share/GeoIP/GeoLite2-Country.mmdb');
$record = $reader->country( $_SERVER['REMOTE_ADDR'] );
try {
$country = $record->country->isoCode;
switch((string)$country) {
case 'US':
$url = "http://www.example.com/us";
break;
case 'MY':
$url = "http://www.example.com/my";
break;
default:
$url = "http://www.example.com";
}
if (strpos("http://$_SERVER[HTTP_HOST]", $url) === false)
{
header("Location: ".$url);
}
} catch (Exception $e) {
// Handle exception
}
?>
Any help is greatly appreciated.
You could use a cookie to keep track of:
if the visitor has been redirected before
the country that the visitor has been redirected to before
If the spiders are clever, they will make use of the cookies too (Ref: Can Bots/Spiders utilize Cookies?).
So you could write your logic like so:
<?php
require_once '../vendor/autoload.php';
use GeoIp2\Database\Reader;
$cookie_name = "country_code";
session_start();
if (isset($_GET['check']) && $_GET['check'] == true) {
if (isset($_COOKIE['test_cookie']) && $_COOKIE['test_cookie'] == 'test') {
if(!isset($_COOKIE[$cookie_name])) {
$reader = new Reader('/usr/local/share/GeoIP/GeoLite2-Country.mmdb');
$record = $reader->country( $_SERVER['REMOTE_ADDR'] );
try {
$country = $record->country->isoCode;
switch((string)$country) {
case 'US':
$url = "http://www.example.com/us";
break;
case 'MY':
$url = "http://www.example.com/my";
break;
default:
$url = "http://www.example.com";
}
$cookie_value = "" . (string)$country;
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
if(!isset($_GET['cookies'])){
header('Location:/info.php?cookies=true');
}
if (strpos("http://$_SERVER[HTTP_HOST]", $url) === false)
{
header("Location: ".$url);
}
} catch (Exception $e) {
// Handle exception
}
} else { //cookie is set no redirect
}
} else { //no cookie support, no redirect
}
} else {
setcookie('test_cookie', 'test', time() + 3600);
header("location: {$_SERVER['PHP_SELF']}?check=true");
}
?>

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

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

PHP GET URL local domain

I have redirect page called get.php which contain the following code:
header('Location: '.urldecode($_GET['url']));
$url = (isset($_GET[url]) && !empty($_GET[url])) ? $_GET[url] : NULL;
if(empty($url)){
header('Location: http://www.example.com/404');
}
This link used for ref tracking. When I check logs, I found someone abused it with pointing to non-malware website ie.
http://www.example.com/get.php?s&url=http://i-am-malware.yes
How to prevent this abused and only accept within local domain.
try this one
$url = "";
if(isset($_GET['url']))
{
$url = urldecode($_GET['url']);
}
if($url=="")
{
header('Location: http://www.example.com/404');
exit;
}
else
{
$arr = parse_url($url);
if($arr['host']==$_SERVER['SERVER_NAME'])
{
header("Location:".$url);
}
else
{
header('Location: http://www.example.com/404');
}
exit;
}

Categories