最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - How to redirect users based on browser language - Stack Overflow

programmeradmin5浏览0评论
  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:");
    
  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?

  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.");
    
  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?

Share Improve this question edited Feb 28, 2010 at 19:12 Gumbo 656k112 gold badges791 silver badges851 bronze badges asked Feb 28, 2010 at 18:57 ZackZack 3971 gold badge8 silver badges16 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 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.");
  }
}

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.

发布评论

评论列表(0)

  1. 暂无评论