How to find website visitor location and language in PHP

I was having trouble finding an easy way to find a website visitors location and language without using various api’s and complex functions so came up with my own little solution. Keep in mind that this is in no way a secure or 100% clean solution but for most needs it should do the trick.

The full solution (I will break it apart later for those that want more detailed explanations)

$clientbrowserheader = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
$browserlanguage = explode(",", $clientbrowserheader);
$langstring = strtolower($browserlanguage[0]);
//echo $langstring;
if ($langstring = "en-us") {
	echo "US VISITOR";
} else {
	echo "NOT US VISITOR";
}

STEP 1: Get website visitors browser header information in PHP
We will start by getting the header information holding the language. When pulling this it will not only pull in the clients browser language but also several other pieces of information that may or may not be useful to you. For this tutorial we only want the browser language and dialect This will show things like United States English or United Kingdom English to help us determine if the user is a US user or some other country that may also speak English.

$clientbrowserheader = $_SERVER['HTTP_ACCEPT_LANGUAGE'];

STEP 2: Break visitors browser information into useable pieces in PHP
Now that we have all of the header information from the visitors browser we need to break it into smaller pieces that we can use. To separate the data saved to the $clientbrowserheader variable we will use the PHP explode function, this is a PHP native function and can be used any time.

$browserlanguage = explode(",", $clientbrowserheader);

The above line simply takes what we already have saved to the $clientbrowserheader variable and saves it in pieces within an array. The data is broken into a new piece every time a comma is reached hence the “,”. This data can then be found by calling any part of the array for example $browserlanguage[0] would be everything before the first comma and $browserlanguage[1] woudl be anything after the first comma and before the second comma etc.

STEP 3: Get website visitors browser language in PHP
In this case the information that we want being the users browser language it is the first value in the array i.e. $browserlanguage[0]. This said all browsers are not created equal and some may see the value as en-us and others may see en-US so we need to make the case consistent by using something like strtolower().

$langstring = strtolower($browserlanguage[0]);

By using the strtolower() native PHP function we have taken the data saved to the $browserlanguage[0] like en-US and made it consistently change the value to all lower case letters i.e. en-us. This makes it cleaner when creating your if statements to show data based on location.

STEP 4: Display different data based on geolocation in PHP
For the sake of this tutorial we only created an if statement saying if someone is in the US show something else show everyone else something else. That said you can easily add another piece to is like elseif $langstring = “en-gb” show the UK site.

if ($langstring = "en-us") {
	echo "US VISITOR";
} else {
	echo "NOT US VISITOR";
}

Conclusion
Basically we are creating a way to make an educated guess on where your visitors are coming from that will almost always work as expected. This is however not a full proof way of determining a visitors location since any user can simply change their browsers language making the page think that they are somewhere else. A more secure way is using IP Gelocating although for those not using this for security but more for convenience this is all you need and works very well. If anyone has input, ideas or even just general questions feel free to drop me a line below.

Leave a Reply

Your email address will not be published. Required fields are marked *