PHP Get browser Language

In php you can get the language of your visitor, this is very helpful if you have many versions of your website for different languages.

You can get the language of your visitor in this way:
<?php
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
?>


This will give you the 2letter language code, ie: en for english and fr for french .

In fact $_SERVER['HTTP_ACCEPT_LANGUAGE'] will get all language the user prefer to use, in order. So substr() will give you the first choice, you can also get the second choice, the third ...

Having the language of your visitor will let you display custom page depending on his language, for example if someone's language is french, you will have $lang="fr" , then you can redirect him to www.yoursite.com/fr/ or yoursite.com/?lang=fr to show him your website in french, or show him things related to french language, whatever your website is about.

9 comments:

Anonymous said...

Thanks for that useful info.

Unknown said...

Our PHP Developers have excellent skills in content management system (CMS), collaborative networking systems, shopping carts, testing, implementations, portals and most important online business application development.

LOL elo boost said...

Amazing because typical! This can be my personal very first time commenting however I've been subsequent your site for a long time: )#) adore the appearance upon EACH from the felines encounters; )#) extremely adorable hair styles, as well.

League of Legends boost


Cheap Fifa 14 Ultimate Team Coins

Anonymous said...

Thanks for this, I have used it as a base for determining a users location and language to show different information to those in the US then those in other countries. You can see it at http://www.brandonorndorff.com/scripts/php/how-to-find-website-visitor-location-and-language/

Unknown said...

Hey Nice blog,Thanks for sharing this blog.!!!


Best Summer training in lucknow

Best Php training in lucknow

Summer training in lucknow

Summer training Company in lucknow

Unknown said...

Really incredible and interesting article. Keep the good work. I really appreciate your effort on putting such thoughts in the post. Thanks for sharing with us. Latest php jobs in hyderabad


JAHAN said...

This is so helpful I will definitely use this and I will happily link your site. I was having such a hard time figuring this privacy stuff out until I found your site. Thanks.

Ancy merina said...
This comment has been removed by the author.
jefrin said...

Very nice to read the blog


blue prism training in chennai