views:

76

answers:

1

I am using following code to redirect users based on browser language:

if(!isset($_SESSION['lang'])){
  if(preg_match('/en-US/', $_SERVER['HTTP_USER_AGENT'])){
    $_SESSION['lang'] = 'en';
    header("location:index.php");
  }else{
    $_SESSION['lang'] = 'other';
    header("location:http://cn.gearor.com");
  }
}

I put the code in my WordPress theme, at the very beginning before DOCTYPE, and I got an error: unexpected '{' in header.php on line 1. I can't figure out what causes this.

Related: http://stackoverflow.com/questions/2352205/how-to-redirect-users-based-on-browser-language

+2  A: 

Can you please supply the code of your header.php? Your post does not tell us where to find line 1 of your code.

Wordpress encourages developers to use the function wp_redirect() for redirecting to other resources instead of sending header() like you would do in php.

For usage see: http://codex.wordpress.org/Function_Reference/wp_redirect

Related: http://wordpressapi.com/solved-header-function-issue-wordpress

codescape
+1 for showing how to stay inside the framework and even linking to the appropriate documentation.
Pekka
I just try to use wp_redirect instead of header, but I can open the page because of too many redirection. Here is my header:if (!isset($_SESSION['lang'])) { if (preg_match('/en-US/', $_SERVER['HTTP_USER_AGENT'])) { $_SESSION['lang'] = 'en'; wp_redirect("http://gearor.com"); } else { $_SESSION['lang'] = 'Other'; wp_redirect("http://cn.gearor.com"); }}
Zack