Rotate Links Using Geoplugin - php

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

Related

Php that redirects to diff URL depending on the param value of the current html page

I really can't find a php regarding what I need. I made an initial php that points to "www.mydomain.com/test/index.html?v=1". Now what I want if when I click a link inside that page, it checks the v value and depending on the value, it redirects it to a specific url. I have 3 v values with specific URLs, also if you can add a code if v is null. thanks a lot.
use
header('location: URL or Link');
with if conditions
if($v==somthing){
header('location: URL or Link');
}elseif($v==$somthing_else){
header('location: URL or Link');
}else{
}
Use header and switch case
May be your header not working , this occurs if you have print something before redirect.
$v = $_GET['v'];
switch($v){
case "1":
header('location: YOUR LINK');
break;
case "2":
header('location: YOUR LINK');
break;
case "3":
header('location: YOUR LINK');
break;
default:
header('location:DEFAULT');
}
O Using if -elseif -else
$v = $_GET['v'];
if($v == 1){
header('location: YOUR LINK');
die;
}
elseif($v == 2){
header('location: YOUR LINK');
die;
}
elseif($v==3){
header('location: YOUR LINK');
die;
}
else{
header('location:DEFAULT LINK');
die;
}

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

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

Categories