/    Sign up×
Community /Pin to ProfileBookmark

Geo redirect script HELP!

Hello everyone, I need help with installing a geo redirect script on my website, what I need to do is redirect US traffic to an US page, UK to a UK page, and so forth…? Im not sure how to modify the code so to have work for me.

The php code is this one:

[code=php]<?php
require_once(‘geoip.inc’);

$gi = geoip_open(‘GeoIP.dat’, GEOIP_MEMORY_CACHE);
$country = geoip_country_code_by_addr($gi, $_SERVER[‘REMOTE_ADDR’]);
geoip_close($gi);

$my_countries = array(‘us’, ‘ca’, ‘gb’, ‘fr’, ‘de’, ‘nl’);
if (!in_array(strtolower($country), $my_countries))
{
header(‘Location: http://www.”ALL”TRAFFICURLGOESHERE.whatever’);
}
else
{
header(‘Location: http://www.”SELECTEDCOUNTRIES”URLGOESHERE.whatever’);
}
?> [/code]

Ive inserted the code and when I go to the domain I see this message:

Warning: Cannot modify header information – headers already sent by (output started at /home/public_html/mydomain.com – Copy.php:7) in /home/public_html/mydomain.com/ on line 21

Thats how Ive inserted the code:

[code=php]<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″ />
<title>My Title</title>

<?php
require_once(‘geoip.inc’);

$gi = geoip_open(‘GeoIP.dat’, GEOIP_MEMORY_CACHE);
$country = geoip_country_code_by_addr($gi, $_SERVER[‘REMOTE_ADDR’]);
geoip_close($gi);

$my_countries = array(‘us’);
if (!in_array(strtolower($country), $my_countries))
{
header(‘Location: US URL goes here’);
}
else
{
header(‘Location: other URLs goes here’);
}
?>
</head>

</body>
</html>[/code]

Thank you in advance!

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@semmelbroeselOct 03.2011 — Hi.

The error message says it all: You have already sent your HTML data, so you cannot switch from showing your page to redirecting to another.

Try this:

[CODE]<?php
require_once('geoip.inc');

$gi = geoip_open('GeoIP.dat', GEOIP_MEMORY_CACHE);
$country = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
geoip_close($gi);

$my_countries = array('us');
if (!in_array(strtolower($country), $my_countries))
{
header('Location: US URL goes here');
}
else
{
header('Location: other URLs goes here');
}
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>My Title</title>
</head>
[/CODE]


Make sure that the <?php tag is the very first item in the file to avoid other issues.

If this method is not an option, you can also go for a javascript version of it:

[CODE]if (!in_array(strtolower($country), $my_countries))
{
echo '<script type="text/javascript">location.href="http://www.newurl.com";</script>';
}
else
{
echo '<script type="text/javascript">location.href="' . $variable_containing_new_url . '";</script>';
}
[/CODE]


Not ideal since javascript can be disabled, but it's a possibility.

I hope this helps.

PS: You're missing the opening BODY tag.
Copy linkTweet thisAlerts:
@SnatchauthorOct 04.2011 — Thanks semmelbroesel, Ive tried that and is no longer giving me the error.

however Im still little bit confused. Im introducing the urls for us and else but it always goes to the "else" url not in the us url. Im currently in Ireland but I have VPN going and I switch eventually to us to check if it will work or not.

So this is the original code:

(Ive got it from here)

[code=php]<?php
require_once('geoip.inc');

$gi = geoip_open('GeoIP.dat', GEOIP_MEMORY_CACHE);
$country = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
geoip_close($gi);

$my_countries = array('us', 'ca', 'gb', 'fr', 'de', 'nl');
if (!in_array(strtolower($country), $my_countries))
{
header('Location: http://www."ALL"TRAFFICURLGOESHERE.whatever');
}
else
{
header('Location: http://www."SELECTEDCOUNTRIES"URLGOESHERE.whatever');
}
?>[/code]


Im not sure where to put the us url and then the other url, in the first place or in the last?

what I need to do is redirect US traffic to an US page, UK to a UK page, and so forth...Thank you for your patience
Copy linkTweet thisAlerts:
@007JulienOct 04.2011 — If your pages are, for example, usindex.html, caindex.html, gbindex.html ...etc. You have only to use this PHP file (index.php)

[code=php]<?php

require_once('geoip.inc');
$gi = geoip_open('GeoIP.dat', GEOIP_MEMORY_CACHE);
$country = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
geoip_close($gi);

$my_countries = array('us', 'ca', 'gb', 'fr', 'de', 'nl');

/* Redirect to a different page in the current directory that was requested */
$host = $_SERVER['HTTP_HOST'];
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\');
$extra = 'index.html';
$page="us".$extra;//or "us$extra";
if (in_array(strtolower($country), $my_countries)) $page=$country.$extra;

header("Location: http://$host$uri/$page");
exit;
?>[/code]
Copy linkTweet thisAlerts:
@SnatchauthorOct 04.2011 — Hi Julien, this is completely different php code, and Im lost...sorry dont understand how to setup this, can you clarify please? Where to insert the us, uk, ca pages exactly? Thank you in advance!
Copy linkTweet thisAlerts:
@007JulienOct 04.2011 — No isn't completely different ! I take only the example of PHP manual for redirection (this page).

PHP run on the server before sending the page. You have only to choose the page before sending.

A better solution would be to redirect only if you have build a specific page with a page like this (index.php) :
[code=php]<?php

require_once('geoip.inc');
$gi = geoip_open('GeoIP.dat', GEOIP_MEMORY_CACHE);
$country = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
geoip_close($gi);

// Our specific pages caindex.html, gbindex.html, frindex.html, deindex.html and nlindex.html
$my_countries = array('ca', 'gb', 'fr', 'de', 'nl');

/* Redirect to a different page in the current directory that was requested */
$host = $_SERVER['HTTP_HOST'];
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\');
$extra = 'index.html';
// if $country is in my_countries your redirect to the specific page
if (in_array(strtolower($country), $my_countries)) {
$page=$country.$extra;
header("Location: http://$host$uri/$page");// this will send the page obtained by concatenating $country and $extra

exit;}
// else you send the default page for (us)
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>My Title</title>
</head>
<body>
<!-- -->
</body>
</html>[/code]


The other pages are in the same directory with there names caindex.html, gbindex.html, frindex.html, deindex.html and gbindex.html.

If you prefer different directory (ca, gb, fr, de and nl with the same index.html), you have only to insert a slash in the adresss : $page=$country.'/'.$extra;
Copy linkTweet thisAlerts:
@SnatchauthorOct 04.2011 — Hi Julien can you please point exactly when to insert the urls? Sorry Im a complete newbie to php. Thank you for your patience!
Copy linkTweet thisAlerts:
@dangerousprinceOct 04.2011 — Basically, the way he's set it up is so that if the country that you're allowing is in the array:

[code=html]$my_countries = array('ca', 'gb', 'fr', 'de', 'nl');[/code]

Then they will forward to this:

[code=html]/* Redirect to a different page in the current directory that was requested */
$host = $_SERVER['HTTP_HOST'];
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\');
$extra = 'index.html';
// if $country is in my_countries your redirect to the specific page
if (in_array(strtolower($country), $my_countries)) {
$page=$country.$extra;
header("Location: http://$host$uri/$page");// this will send the page obtained by concatenating $country and $extra

exit;}
// else you send the default page for (us)[/code]


And if you read it, is says that whatever country, for example, myself - 'gb', I will be redirected to the domain I'm on (so if the site was hosted on google.co.uk, that's what the host is and any additional folders is the uri) it will go to a folder called 'gb' with the extra 'index.html'.

So if I was on http://www.google.co.uk/register

And I was in Great Britain (which I am!)

I would be directed to:

http://www.google.co.uk/register/gb/index.html
Copy linkTweet thisAlerts:
@007JulienOct 04.2011 — I (and all scripts) insert the url in this line :[code=php]
header("Location: ... ");[/code]

Your model with [I]http://www."ALL"TRAFFICURLGOESHERE.whatever[/I] or [I]http://www."SELECTEDCOUNTRIES"URLGOESHERE.whatever[/I] should be adapted to your pages...

I use a method to rewrite the address with server predefined variables (so the script will be valid anyway).

For example if your page address is http://www.mysite.com/test/index.php
[code=php]$host = $_SERVER['HTTP_HOST']; // give www.mysite.com
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\'); // give the directory /test (without / or at the end)[/code]

with a variable $country (fr), a $extra (index.html) and a $page (frindex.html or fr/index.html)
[code=php]
header("Location: http://$host$uri/$page");[/code]

Will redirect to the page http://www.mysite.com/text/frindex.html or http://www.mysite.com/text/fr/index.html

I have to add : there are two methods to concatenate strings in php

The point $chn=$chn1.$chn2; or 'fr'.'index.html' or "fr"."index.html";

and an insertion with double quotes "Location: http://$host$uri/$page" witch insert $host $uri and $page in the initial string.
Copy linkTweet thisAlerts:
@dangerousprinceOct 04.2011 — I (and all scripts) insert the url in this line :[code=php]
header("Location: ... ");[/code]

Your model with [I]http://www."ALL"TRAFFICURLGOESHERE.whatever[/I] or [I]http://www."SELECTEDCOUNTRIES"URLGOESHERE.whatever[/I] should be adapted to your pages...

I use a method to rewrite the address with server predefined variables (so the script will be valid anyway).

For example if your page address is http://www.mysite.com/test/index.php
[code=php]$host = $_SERVER['HTTP_HOST']; // give www.mysite.com
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\'); // give the directory /test (without / or at the end)[/code]

with a variable $country (fr), a $extra (index.html) and a $page (frindex.html or fr/index.html)
[code=php]
header("Location: http://$host$uri/$page");[/code]

Will redirect to the page http://www.mysite.com/text/frindex.html or http://www.mysite.com/text/fr/index.html

I have to add : there are two methods to concatenate strings in php

The point $chn=$chn1.$chn2; or 'fr'.'index.html' or "fr"."index.html";

and an insertion with double quotes "Location: http://$host$uri/$page" witch insert $host $uri and $page in the initial string.[/QUOTE]


Did I not just explain that?
Copy linkTweet thisAlerts:
@007JulienOct 04.2011 — Sorry, I have not seen your good answer.
×

Success!

Help @Snatch spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...