How can PHP beginners effectively implement an IP query for language selection on a website?

To implement an IP query for language selection on a website, beginners can use a free IP geolocation API to retrieve the visitor's country based on their IP address. Once the country is identified, the website can then redirect the user to the appropriate language version of the site.

<?php
$ip = $_SERVER['REMOTE_ADDR'];
$api_url = "https://api.ipgeolocation.io/ipgeo?apiKey=YOUR_API_KEY&ip=$ip";
$data = json_decode(file_get_contents($api_url));

$country_code = $data->country_code;

if($country_code == "US") {
    header('Location: /en-us/');
} elseif($country_code == "FR") {
    header('Location: /fr-fr/');
} else {
    header('Location: /default-language/');
}
?>