Auto-redirect based on location

Status
Not open for further replies.

a!!!!1

Banned
Apr 20, 2008
508
7
0
I'm looking at doing some media buys for mobile offers but seeing as they're all country-specific, I could potentially be wasting a lot of clicks, for example if I link to a US offer and a non-US user clicks it.

I'd like to create a very quick redirect that would check a user's country and then send them to a corresponding offer for their country. I would put in all of the links manually and each country would only need one link. Is there any way to do this? Thanks ahead.
 


download this: geoPlugin's free and easy PHP geolocation webservice explained

put in same folder as this:

ccr.php
Code:
<?php
// ccr.php - country code redirect
require_once('geoplugin.class.php');
$geoplugin = new geoPlugin();

$country_code = $geoplugin->countryCode;

switch($country_code) {
    case 'US':
        header('Location: http://offer.com/united-states');
        exit;
    case 'UK':
        header('Location: http://offer.com/united-kingdom');
        exit;
    default: // exceptions
        header('Location: http://offer.com/generic');
        exit;
}

?>

You can add cases for any country via two letter country codes:
2-Character Alphabetical Country Codes - Digraphs of All Countries

Sean
 
  • Like
Reactions: Rage9
Awesome, +rep.

download this: geoPlugin's free and easy PHP geolocation webservice explained

put in same folder as this:

ccr.php
Code:
<?php
// ccr.php - country code redirect
require_once('geoplugin.class.php');
$geoplugin = new geoPlugin();

$country_code = $geoplugin->countryCode;

switch($country_code) {
    case 'US':
        header('Location: http://offer.com/united-states');
        exit;
    case 'UK':
        header('Location: http://offer.com/united-kingdom');
        exit;
    default: // exceptions
        header('Location: http://offer.com/generic');
        exit;
}

?>
You can add cases for any country via two letter country codes:
2-Character Alphabetical Country Codes - Digraphs of All Countries

Sean
 
I'm in the US but it seems like a lot of the time it sends me to the "else" URL. You know why it's doing that?
 
I think it's redirecting all US IP's, the offer I have for US got 0 clicks today and the offer I have for "else" got a lot more than usual.
 
I think it's redirecting all US IP's, the offer I have for US got 0 clicks today and the offer I have for "else" got a lot more than usual.

Edit: Actually it's sending EVERYONE to the "exceptions" link.
 
Oops, my bad...

Right after:
$geoplugin = new geoPlugin();

Add:
$geoplugin->locate();

Then it should work.

You can use the script below to display your location, including city and state (region). Definitely test it out, I think the code is right but I'm in Bocas del Toro right now and it's only picking up Panama (PA) but not a city or region.

Code:
<?php
// mylocation.php - what is my location?

require_once('geoplugin.class.php');
$geoplugin = new geoPlugin();
$geoplugin->locate();

$geo_country = $geoplugin->countryName;
$geo_country_code = $geoplugin->countryCode;
$geo_city = $geoplugin->city;
$geo_region = $geoplugin->region;   

$geo_location = $geo_country . ' ('. $geo_country_code . ')' . ' - ' . $geo_city . ', ' . $geo_region;

echo "Your location is: " . $geo_location;

?>

Sean
 
Oops, my bad...

Right after:
$geoplugin = new geoPlugin();

Add:
$geoplugin->locate();

Then it should work.

You can use the script below to display your location, including city and state (region). Definitely test it out, I think the code is right but I'm in Bocas del Toro right now and it's only picking up Panama (PA) but not a city or region.

Ah there we go, looks like it's working perfectly now. Thanks man.
 
Status
Not open for further replies.