How to redirect users based on browser language

  • I want to redirect users based on the browser language, I figured out how to do it 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"); 
  • The only problem is that I want to redirect users from other sites or the first time I visit my website. This means that I do not want users to read some pages of my site, when they returned to the index, they were redirected to another page. And I also have a link on my website, for example: English or Chinese, when users click on English from a Chinese page, they will go to index.php and redirect back to the Chinese page, so users cannot visit the English page, How fix this in php or javascript?

+4
source share
2 answers

You can set the session variable as $ _SESSION ['lang'], and apply this code only if $ _SESSION ['lang'] is not set. I mean:

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

Well, it was hard to understand what you were saying, if you can repeat a little, but that’s what I think you asked for, Pastebin .

-1
source

Source: https://habr.com/ru/post/1302679/


All Articles