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.

10 comments:

Anonymous said...

Thanks for that useful info.

rize wordpress 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/

Lucky Srivastava said...

Hey Very nice Blog,Thanks For Sharing..

Python training in Lucknow
Softpro Learning Center
best Php training center
Best python training in lucknow

Lucky Srivastava 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

sai pallavi 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.

ravi kumar 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.

White Label Website Builder

jhansi joe said...

Great and impressive article!! Got to learn and know more about web development. To know more refer create website for much more unique ideas.PHP Training in Chennai | Best PHP training in Chennai