We can detect requesting browser’s language using PHP’s super global variable
$_SERVER. It is a superglobal variable which holds information about headers, paths, and script locations. It is basically an associative array in PHP which has keys like
SERVER_NAME, SERVER_ADDR, REQUEST_METHOD, etc.
We can use
HTTP_ACCEPT_LANGUAGE key to get the language of the browser.
We can see an output like:
en-US, en;q=0.9, hi;q=0.8, fr;q=0.7
In order to get the current language of the browser, we can use PHP’s built-in substr function to get the first two letters of the string like-
After running the above program you’ll see the output as your current browser’s language –
You can test it by changing your browser’s language. If you are on chrome you can go to
chrome://settings/languages and choose a different language.
Now run the above program again and you’ll see the output as the newly chosen language.
Example 2: If your website has different pages for different languages, you can use this method in order to redirect to the page according to the user’s browser’s language.
The above program will redirect to links like
- How to detect HTML 5 is supported or not in the browser ?
- How to open a PDF files in web browser using PHP?
- Difference Between Machine Language and Assembly Language
- Browser Automation Using Selenium
- How to give a div tag 100% height of the browser window using CSS
- How to disable browser Autocomplete on web form field/input tag?
- How to jump to top of browser page using jQuery?
- How to get the size of screen, current web page and browser window using jQuery?
- Difference between Web Browser and Web Server
- How to share code between Node.js and the browser?
- How to check the OffScreenCanvas is supported by the Browser or not ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.