[Request] PHP Geo Redirect

MandatoryMagic

Registered Member
Joined
Jul 10, 2009
Messages
91
Reaction score
40
Something similar to the second one in this thread http://www.blackhatworld.com/blackhat-seo/making-money/92299-simplest-geo-redirect-code.html

Code:
<script language="JavaScript" src="http://j.maxmind.com/app/geoip.js"></script>
<script language="JavaScript">

var country = geoip_country_code();

switch (country)
{
case (country = "US"):
  window.location = "YOUR US OFFER";
break;
case (country = "UK"):
  window.location = "YOUR UK OFFER";
break;
default:
  window.location = "YOUR INTERNATIONAL OFFER";
break;
}
</script>

BUT in PHP. I'm only requesting this because many people disable javascript.

So if someone can code this up real quick or can point me in the direction of a PHP Geo Redirect that would be lovely.
 
GodCPA uses geo targeting that is actually quite simple. Or just google geo targeting
Posted via Mobile Device
 
I notice though that it is used in an iframe. Is it possible to locate the code just for Geo Redirecting *without* the iframe.
 
hey, checkout www.geoio.com

Its as simple as calling :
Code:
[URL="http://api.geoio.com/q.php?key=5NPWwjiBd6J6LpKd&qt=geoip&d=pipe&q=VisitorIP"]http://api.geoio.com/q.php?key=secretkeyyougetfromsite&qt=geoip&d=pipe&q=VisitorIP[/URL]
So if I call
Code:
[URL="http://api.geoio.com/q.php?key=5NPWwjiBd6J6LpKd&qt=geoip&d=pipe&q=68.151.233.111"]http://api.geoio.com/q.php?key=secretkeyyougetfromsite&qt=geoip&d=pipe&q=68.151.233.111[/URL]
it returns
Code:
Lloydminster|Alberta|Canada|Shaw Communications Inc|53.28|-110
Now with php just do an explode() with that string and | as the seperator and you will end up with an array where array(2) would = Canada

From there on you can just do the redirect which is easy.
 
If it's easy could you please post the code here. Sorry man but I just suck at PHP. Please, it would be a huge help to me!
 
Here's the basic code you need to do this with PHP using the GeoIO API...

First, setup an account with GeoIO, then log in, click API, and create a new API call by choosing:

Query Type: IP Address to Geographical Location
Delimiter: Pipe (|)

Then copy and paste the whole URL from the text box...it should look something like this:

PHP:
http://api.geoio.com/q.php?key=XXXXXXXXXXX&qt=geoip&d=pipe&q=YOUR QUERY HERE

That line is what you will paste into the code for "api_url" -- Here is the code, hope it helps:

PHP:
<?php

//	adjust the following lines as needed...
$us_offer_url	= "http://URL_TO_USA_LANDING_PAGE.com";
$uk_offer_url	= "http://URL_TO_UK_LANDING_PAGE.com";
$intl_offer_url	= "http://URL_TO_INTERNATIONAL_LANDING_PAGE.com";

//	paste the URL from the geoio backend...
$api_url = "http://api.geoio.com/q.php?key=XXXXXXXXXXXX&qt=geoip&d=pipe&q=";

//	DONT EDIT BELOW HERE UNLESS YOU KNOW PHP...
$api_url .= $_SERVER['REMOTE_ADDR'];
$f = explode("|", file_get_contents($api_url));

if(trim($f[2]) == "United States") {
	header("Location: $us_offer_url");
} elseif(trim($f[2]) == "United Kingdom") {
	header("Location: $uk_offer_url");
} else {
	header("Location: $intl_offer_url");
}

exit();

?>
 
Last edited:
That's awesome skweekykleen! Just one question... How would you add more countries? I assume you do another else if and then add the url at the top but then the whole "trim" thing confused me. Could you please put an example with 2 more countries, for instance GB and CA?
 
Oh snap I think I got it. Tell me if this is write if you want to add GB and CA for instance. *I know GB is UK it's just an example for adding more countries...*

Also why do you put in titles like writing out "United States"?

PHP:
<?php

//    adjust the following lines as needed...
$us_offer_url    = "http://URL_TO_USA_LANDING_PAGE.com";
$uk_offer_url    = "http://URL_TO_UK_LANDING_PAGE.com";
$gb_offer_url    = "http://URL_TO_GB_LANDING_PAGE.com";
$ca_offer_url    = "http://URL_TO_CA_LANDING_PAGE.com";
$intl_offer_url    = "http://URL_TO_INTERNATIONAL_LANDING_PAGE.com";

//    paste the URL from the geoio backend...
$api_url = "http://api.geoio.com/q.php?key=XXXXXXXXXXXX&qt=geoip&d=pipe&q=";

//    DONT EDIT BELOW HERE UNLESS YOU KNOW PHP...
$api_url .= $_SERVER['REMOTE_ADDR'];
$f = explode("|", file_get_contents($api_url));

if(trim($f[2]) == "United States") {
    header("Location: $us_offer_url");
} elseif(trim($f[2]) == "United Kingdom") {
    header("Location: $uk_offer_url");
} elseif(trim($f[2]) == "GB") {
    header("Location: $gb_offer_url");
} elseif(trim($f[2]) == "Canada") {
    header("Location: $ca_offer_url");

} else {
    header("Location: $intl_offer_url");
}

exit();
 
www.geoio.com seems pretty inaccurate for the UK, (It got my location and provider completely wrong...)

Does anyone know of a free alternative that is good for the UK?
Cheers
 
This is awesome got it working without a problem. However, they do charge for anything over 1000 hits a day. Is there any other site which is free or can we run a script on our server that does something similar?
 
Something similar to the second one in this thread http://www.blackhatworld.com/blackhat-seo/making-money/92299-simplest-geo-redirect-code.html

BUT in PHP. I'm only requesting this because many people disable javascript.

So if someone can code this up real quick or can point me in the direction of a PHP Geo Redirect that would be lovely.

you can try this code with the geolite from maxmind
PHP:
require ('geoip.inc');
$gi = geoip_open('GeoIP.dat',GEOIP_STANDARD);
$country_code = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);

if $country_code="US"
header("Location: http://your_us_offer");

elseif $country_code="UK"
header("Location: http:/your_uk_offer");

# if user is not from us or uk, send him international offer
else header("Location: http://your_international_offer");
 
Here's the basic code you need to do this with PHP using the GeoIO API...

First, setup an account with GeoIO, then log in, click API, and create a new API call by choosing:

Query Type: IP Address to Geographical Location
Delimiter: Pipe (|)

Then copy and paste the whole URL from the text box...it should look something like this:

PHP:
http://api.geoio.com/q.php?key=XXXXXXXXXXX&qt=geoip&d=pipe&q=YOUR QUERY HERE

That line is what you will paste into the code for "api_url" -- Here is the code, hope it helps:

PHP:
<?php

//	adjust the following lines as needed...
$us_offer_url	= "http://URL_TO_USA_LANDING_PAGE.com";
$uk_offer_url	= "http://URL_TO_UK_LANDING_PAGE.com";
$intl_offer_url	= "http://URL_TO_INTERNATIONAL_LANDING_PAGE.com";

//	paste the URL from the geoio backend...
$api_url = "http://api.geoio.com/q.php?key=XXXXXXXXXXXX&qt=geoip&d=pipe&q=";

//	DONT EDIT BELOW HERE UNLESS YOU KNOW PHP...
$api_url .= $_SERVER['REMOTE_ADDR'];
$f = explode("|", file_get_contents($api_url));

if(trim($f[2]) == "United States") {
	header("Location: $us_offer_url");
} elseif(trim($f[2]) == "United Kingdom") {
	header("Location: $uk_offer_url");
} else {
	header("Location: $intl_offer_url");
}

exit();

?>

Your code woks. But how do I modify so it redirects only US visitors and keeps all others on page where code is inserted (I dont want to redirect the others in any way)

Help would be appreciated.
 
Back
Top