如何基于浏览器语言重定向用户 [英] How to redirect users based on browser language

查看:97
本文介绍了如何基于浏览器语言重定向用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  1. 我想基于浏览器语言重定向用户,我在这里弄清楚了如何做到这一点,它工作正常,这是代码(PHP):

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

  • 问题是我只想将用户从其他网站重定向或在第一次访问我的网站时重定向.这意味着我不希望用户阅读我网站的某些页面,当他们返回索引时,他们被重定向到其他页面.我的网站上也有一个链接,例如:英文或中文,当用户从中文页面单击英文时,他们将转到index.php并被重定向回中文页面,这使用户无法访问英文页面. .如何在PHP或JavaScript中解决此问题?

  • 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?

    推荐答案

    您可以设置$ _SESSION ['lang']之类的会话变量,并且仅在未设置$ _SESSION ['lang']的情况下应用上述代码. 我的意思是:

    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");
      }
    }
    

    这篇关于如何基于浏览器语言重定向用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆