Override header redirection

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rai
Forum Newbie
Posts: 5
Joined: Sat Sep 25, 2010 5:55 am

Override header redirection

Post 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
robnet
Forum Commoner
Posts: 85
Joined: Mon Aug 10, 2009 8:32 am
Location: South East, UK

Re: Override header redirection

Post 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.
rai
Forum Newbie
Posts: 5
Joined: Sat Sep 25, 2010 5:55 am

Re: Override header redirection

Post 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.
robnet
Forum Commoner
Posts: 85
Joined: Mon Aug 10, 2009 8:32 am
Location: South East, UK

Re: Override header redirection

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Override header redirection

Post 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();
User avatar
DigitalMind
Forum Contributor
Posts: 152
Joined: Mon Sep 27, 2010 2:27 am
Location: Ukraine, Kharkov

Re: Override header redirection

Post 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'];
}
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Override header redirection

Post 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
} 
Post Reply