Page 1 of 1

Override header redirection

Posted: Mon Sep 27, 2010 7:01 am
by rai
I have the following code:

Code: Select all

if($CountryName=='China'){
header('Location: page1.php');
}
else
{
header('Location: page2.php');
} 
Page 1 is in Chinese. Page 2 is in English.

If an English speaking customer who resides in China visited the website, they would automatically be taken to the Chinese language version based on their IP address being identified as in China. However, they may wish to view the website in English.

My question is how do I override the redirection to page 1 if the customer wants to view the website in English?

Any help greatly appreciated and thanks in advance.

R

Re: Override header redirection

Posted: Mon Sep 27, 2010 7:11 am
by robnet
How about having a couple of links on site for different languages.

Code: Select all

<a href='?lang=EN'>English</a>
<a href='?lang=CN'>Chinese</a>
Then add this above page redirection section

Code: Select all

if($_GET['LANG']=='EN'){
  $CountryName='UK';
} elseif($_GET['LANG']=='CN') {
  $CountryName='China';
}

if($CountryName=='China'){
header('Location: page1.php');
}
else
{
header('Location: page2.php');
}
Depending on how you initially specify $CountryName you might also want to add the selected country as a Session/Cookie Var.

Re: Override header redirection

Posted: Mon Sep 27, 2010 1:58 pm
by rai
Thanks robnet.

That's certainly an option to consider. However, I don't really want the customer to initially have to select their language. I want to be able to redirect automatically before the customer has to do anything based on the customer's location with an override option should the customer wish at that point to change the language.

I know that I will probably need to use sessions, but I don't really know how to go about it.

Any further advice again greatly appreciated.

Re: Override header redirection

Posted: Mon Sep 27, 2010 4:07 pm
by robnet
The customer doesn't have to specify a language. Do an extra check - if !isset($_GET['LANG']) then you don't assign a new value to $CountryName, you can keep whichever value you've previously worked out based on IP.

Re: Override header redirection

Posted: Mon Sep 27, 2010 4:16 pm
by John Cartwright
When setting the language (you first want to check for the override in the URL) -- then -- check to see if we have previously had it set, and if not, determine their location based on ip.

Code: Select all

session_start();

if (!isset($_GET['lang'])) {
   $_SESSION['lang'] = $_GET['lang'];
} else if (!isset($_SESSION['lang'])) {
   $_SESSION['lang'] = get_language_preference(); //your magic geoip function here
} 
And when deciding where to redirect them..

Code: Select all

if (isset($_SESSION['lang']) && $_SESSION['lang'] == 'cantonese') {
   header('Location: /page1.php');
} else {
   header('Location: /page2.php'); 
}
exit();

Re: Override header redirection

Posted: Mon Sep 27, 2010 4:22 pm
by DigitalMind
John Cartwright wrote:

Code: Select all

session_start();

if (!isset($_GET['lang'])) {
   $_SESSION['lang'] = $_GET['lang'];
} else if (!isset($_SESSION['lang'])) {
   $_SESSION['lang'] = get_language_preference(); //your magic geoip function here
} 
looks like you have made a mistake
if (!isset($_GET['lang'])) { means if $_GET['lang'] doesn't exist
$_SESSION['lang'] = $_GET['lang'];
}

Re: Override header redirection

Posted: Mon Sep 27, 2010 4:23 pm
by John Cartwright
Right. Remove the negate operator.

Code: Select all

session_start();

if (isset($_GET['lang'])) {
   $_SESSION['lang'] = $_GET['lang'];
} else if (!isset($_SESSION['lang'])) {
   $_SESSION['lang'] = get_language_preference(); //your magic geoip function here
}