5
  1. I want to redirect users based on browser language, I figured out how to do this here, it works fine, here is the code(PHP):

    if(preg_match('/en-US/', $_SERVER['HTTP_USER_AGENT']))
        header("location:index.php");
    else
        header("location:http://cn.gearor.com");
    
  2. The problem is I only want to redirect users from other websites or at the first visit to my website. Which means I don't want users reading some pages of my website, when they go back to index, they were redirect to other page. And I also have a link in my website, like: English or Chinese, when users click English from a Chinese page, they will go to index.php and been redirect back to Chinese page, it makes users can't visit the English page. How do I fix this in PHP or JavaScript?

Gumbo
  • 643,351
  • 109
  • 780
  • 844
Zack
  • 397
  • 1
  • 8
  • 16

2 Answers2

6

You can set a session variable like $_SESSION['lang'] and only apply the above code if $_SESSION['lang'] is not set. what I mean is:

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");
  }
}
Sinan
  • 5,819
  • 11
  • 39
  • 66
  • Thanks Sinan, this is what I am looking for. But when I put your code into my website, I got an error of unexpected '{' in header.php in line 1. I am using Wordpress, I put the code in the very beginning of my theme before the " – Zack Mar 01 '10 at 02:34
  • @Zack Are you sure you want to do this for every single page and post in your worpdress site? If so, put it in the index.php instead. Inside tags, of course. – InanisAtheos Aug 15 '13 at 14:25
-1

Well it was hard to understant what you are saying, if you can reclarifiy a bit, but here is what I think you were asking for, Pastebin.

Dr Hydralisk
  • 1,171
  • 4
  • 14
  • 22